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.
Design By Canva
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);
}
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);
}
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);
}
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;
}
#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;
}
#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;
}
#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;
}
#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);
}
SL No. | My Invited Steemit Friends |
---|---|
1 | @memamun |
2 | @shahariar1 |
3 | @solaymann |
This is my Twitter share link :
https://twitter.com/mahadih83660186/status/1858001960508731813?t=ubADzZobaK6AmFamtakQAQ&s=19
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit