EXAMPLES OF STRING
WAP to read a string. Find its length.
#include<stdio.h>
void main()
{
char str[100];
int len;
/// read the string
printf(“Enter a string :”);
scanf(“%s”,&str);
///find the length
for( len=0;str[len]!='\0';len++);
printf(“Length=%d”,len);
}
Output:
Enter a string: ramana
Length=6
WAP to read a string. Print its reverse.
#include<stdio.h>
void main()
{
char str[100],rev[100];
int i,j,len;
/// read a string
printf(“Enter a string :”);
scanf(“%s”,&str);
///reverse the string.
for( len=0;str[len]!='\0';len++);
for( i=0,j=len-1;str[i]!='\0';i++, j-- )
rev[j]=str[i];
rev[len]='\0';
printf(“Reverse=%s”,rev);
}
Output:
Enter a string: college
Reverse= egelloc
WAP to read a string. Print whether the string is palindrome or not.
#include<stdio.h>
void main()
{
char str[100],rev[100];
int i,j,len,flag=1;
/// read a string
printf(“Enter a string :”);
scanf(“%s”,&str);
///reverse the string
for( len=0;str[len]!='\0';len++);
for(i=0,j=len-1;str[i]!='\0';i++,j--)
rev[j]=str[i];
rev[len]='\0';
for(i=0;str[i]!='\0';i++)
if( str[i] != rev[i] )
flag=0;
if( flag==1 )
printf(“Palindrome”);
else
printf(“Not a palindrome”);
}
Output1:
Enter a string: dad
Palindrome
Output2:
Enter a string: nrt
Not a palindrome
WAP to read a string, convert it to upper case.
#include<stdio.h>
void main()
{
char str[100];
int i;
printf(“Enter a string :”);
scanf("%s",&str);
for( i=0;str[i]!='\0';i++)
{
if( str[i]>='a' && str[i]<='z' )
str[i]=str[i]-32;
}
printf(“result=%s”,str);
}
Output1:
Enter a string: RAmana
result=RAMANA
WAP to read a string. convert it to lower case.
#include<stdio.h>
void main()
{
char str[100];
int i;
printf(“Enter a string :”);
scanf(“%s”,&str);
for( i=0;str[i]!='\0';i++)
{
if( str[i]>='A' && str[i]<='Z' )
str[i]=str[i]+32;
}
printf(“result=%s”,str);
}
Output1:
Enter a string: RAmana
result= ramana
WAP to read a string. Count the alphabets, vowels, consonants, spaces, digits, symbol.
#include<stdio.h>
void main()
{
char str[100];
int ac=0,vc=0,cc=0,sc=0,dc=0,ssc=0,i;
printf(“Enter a string :”);
gets(str);
for(i=0;str[i]!='\0';i++)
{
if( (str[i]>='A'&&str[i]<='Z') || (str[i]>='a'&&str[i]<='z'))
{
ac++;
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='E'||str[i]=='I'||str[I ]=='O'||str[i]=='U')
vc++;
else
cc++;
}
else if( str[i]==' ')
sc++;
else if( str[i]>='0'&&str[i]<='9')
dc++;
else
ssc++;
}
printf(“Alphabets count=%d”,ac);
printf(“\nVowels count=%d”,vc);
printf(“\nConsonants count=%d”,cc);
printf(“\nDigits count=%d”,dc);
printf(“\nSpaces=%d”,sc);
printf(“\nSymbols=%d”,ssc);
}
Output :
Enter a string : rama,raman are brothers. rama got 80%. raman got 75%.
Alphabets count=35
Vowels count=14
Consonants count=21
Digits count=4
Spaces=8