SLC21 Week3 - Strings in C

in hive-170554 •  13 hours ago 

Steemian Friends,
Today, I will write @sergeyk sir's homework for week 03 of Steemit Learning Challenges. The homework topic is SLC21 Week 3 - Strings in C. Completed the homework and got to practice programming. I hope everyone likes my homework.

Strings in C.png
Design By Canva

(3 points) Practically (i.e., with code examples) explain the theory from the first part of this lesson, where the concept of two sizes of an array is discussed. Demonstrate how to make it look like the array size can be increased/decreased. All loops should work with the array size stored in the size variable. Keep the physical, actual size in the constant N.

I will discuss String in three steps. First, I created the structure of the C++ program, which is below.

#include<stdio.h>
int main()
{
}

The string is a character-type array. The character type is called string. First, I declared a character-type variable. Here, we can keep only one character in the variable called ch.

char ch ;
ch='a' ;

However, I will use a string to store multiple characters. Below are the rules for declaring a string.

char s1[7];

There are indexes named S1, S2, S3, S4,S5, and S6. I can put the characters in each index. I have given the technique of keeping each index character below.

S1[0]='M' ;
S2[1]='A';
S2[2]='H';
S2[3]='A';
S2[4]='D';

S2[5]='I';

A null character must be used at the end of the program to end it in a string. The compiler will then terminate the program. I have shown declaring the null character below.

S1[6]='\0';

Then, we write the following lite to see the output.

printf ( s1 =" %s", s1);

Now, I have written the entire program, which is below.

#include<stdio.h>
int main()
{
char s1[7];
S1[0] = 'M';
S1[1] = 'A';
S1[2] = 'H';
S1[3] = 'A';
S1[4] = 'D';
S1[5] = 'I';
S1[6] = '\0';
printf(" s1 = %s\n", s1);
}

Below, I have shown the while loop condition within a string.

while (str1[i]!='\0')
{
i++;
len++;
}

Below, I have shown the for loop condition within a string. I have used while and for loop in the answers to the following questions.

for(j=0,i=len-1; i>=0; i--,j++)
{
str2[j] = str1[i];
}

Now, I can directly initialize the string. I have shown the direct initialization below.

char s1[7] = { 'M', 'A','H','A','D','I', '\0'};
#include<stdio.h>
int main()
{
char s1[7] = { 'M', 'A','H','A','D','I', '\0'};
printf(" s1 = %s\n", s1);
}

string1.png

I can declare the string in double quotes if I want. Below, I have declared the string in double quotes.

char s1[]= " Mahadi Hasan" ;

#include<stdio.h>
int main()
{
char s1[]= " Mahadi Hasan" ;
printf(" s1 = %s\n", s1);
}

string2.png


(1 point) Declare a string variable (store any sentence in the array). Task: reverse the string, i.e., write it backward. For example: char s[]="ABCDEF";.....your code.....cout<<s; => FEDCBA

I have declared the size of the string as 30. Even if we don't give the size of the string. First, we used a while loop to find the length of the string. Then, we use a for loop to do the reverse. We have indexed zero to indicate the end of the compiler program. Finally, we use printf to view the program's output. Below is the program and its output.

#include<stdio.h>
int main()
{
char str1[30]= "ABCDEF";
char str2[30];
int i=0,len=0,j;
while (str1[i]!='\0')
{
i++;
len++;
}
for(j=0,i=len-1; i>=0; i--,j++)
{
str2[j] = str1[i];
}
str2[j] = '\0';
printf("str1 = %s\n",str1);
printf("str2 = %s\n",str2);
}

string4.png


(1 point) Swap neighboring letters char s[]="ABCDEF";.....your code.....cout<<s; => BADCFE

In the program, I wrote the program to fetch the second string first. First, I declared a string. Then, I bring the second character to the first using i and length variables through the for loop. As usual, I have shown the program's output.

#include < iostream>
#include< cstring>
using namespace std;
int main() {
char s[] = "ABCDEF";
cout << s << endl;
int length = strlen(s);
for(int i = 0; i< length - 1; i += 2)
{
char str = s[i];
s[i] = s[i + 1];
s[i + 1] = str;
}
cout << s << endl;
return 0;
}

string5.png


(1.5 points) Shift the string cyclically to the left (it’s easier to start with this), then cyclically to the right. char s[]="ABCDEF", x[]="abrakadabra";.....your code.....cout<<s<<"\n"<<x; => BCDEFA aabrakadabr

#include< stdio.h>
#include< string.h>
int main()
{
char str[20];
int i;
printf("\n Enter the string: ");
scanf("%s",str);
printf("\n string: %s",str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("\n string: %s",str);
return 0;
}

upercase.png

#include< stdio.h>
#include< string.h>
int main()
{
char str[20];
int i;
printf("\n Enter the string: ");
scanf("%s",str);
printf("\n string: %s",str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\n string is in lower case: %s",str);
return 0;
}

lower case.png


(1.5 points) Remove all vowel letters char s[]="this is some text";...your code...cout<<s; => ths s sm txt

#include < iostream>
#include< cstring>
using namespace std;

bool isvowel(char ch)
{
ch = tolower(ch);
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
}
int main()
{
char s[] = "this is a book";
int length = strlen(s);
int index = 0;
for (int i = 0; i< length; i++)
{
if (!isvowel(s[i]))
{
s[index++] = s[i];
}
}
s[index] = '\0';
cout << s << endl;
return 0;
}

vowelstring1.png


(2 points) Double each vowel letter char s[]="this is some text";...your code...cout<<s; => thiis iis soomee teext

#include < stdio.h>
#include< string>
int main()
{
char s1[] = "Mahadi hasan";
int i = 0, len = 0;
while (s1[i] != '\0')
{
i++;
len++;
}
printf("Length = %d\n",len);

}

length.png

Blue line.png

SL No.My Invited Steemit Friends
1@memamun
2@shahariar1
3@solaymann

Steemit.com.png

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order: