Character Array (String)
1. Write down the C program to find the length of string.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char string[30];
int len;
printf("Enter a string:\n");
scanf("%s",string);
len=0;
while (string[len]!='\0')
{
len++;
}
printf("The string \"%s\" has %d characters.\n",string,len);
return 0;
}
2. Write down the C program to concatenate two string.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char source[30], target[30];
printf("Enter a source string:\n");
scanf("%s",source);
printf("Enter a target string:\n");
scanf("%s",target);
strcat(target,source);
printf("The concatenated string is:%s",target);
return 0;
}
3. Write down the C program to compare two string.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char s1[30], s2[30];
int y;
printf("Enter a string1:\n");
scanf("%s",s1);
printf("Enter a string2:\n");
scanf("%s",s2);
y=strcmp(s1,s2);
if(y==0)
printf("strings are are equal.");
else if(y==-1)
printf("The number of characters in string1 is lesser than string2");
else
printf("The number of characters in string1 is greater than string2");
return 0;
}
4. Write down the C program to reverse the string.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char source[30];
printf("Enter a source string:\n");
scanf("%s",source);
strrev(source);
printf("The reverse string is:%s",source);
return 0;
}
5. Write down the C program to copy the String.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char s1[30], s2[30];
printf("Enter a string1:\n");
scanf("%s",s1);
printf("Enter a string2:\n");
scanf("%s",s2);
strcpy(s2,s1);
printf("The copied string is %s",s2);
return 0;
}
6. Write down the C program to make the string in lowercase.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char source[30];
printf("Enter a Uppercase string:\n");
scanf("%s",source);
strlwr(source);
printf("The lowercase string is:%s",source);
return 0;
}
7. Write down the C program to make the string in uppercase.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char source[30];
printf("Enter a lowercase string:\n");
scanf("%s",source);
strupr(source);
printf("The uppercase string is:%s",source);
return 0;
}
8. Write down the C program to count the number of vowels in a string.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char string[30];
int i,k, count=0;
printf("Enter a string:\n");
scanf("%s",string);
k=strlen(string);
for(i=0;i<k;i++)
{
if(string[i]=='a'||string[i]=='A'||string[i]=='e'||string[i]=='E'||string[i]=='i'||
string[i]=='I'||string[i]=='o'||string[i]=='O'||string[i]=='u'||string[i]=='U')
{
count=count+1;
}
}
printf("The number of vowels is:%d",count);
return 0;
}
9. Write down the C program to check the entered string is palindrome or not.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char word[30], word1[30];
printf("Enter a word:\n");
scanf("%s",word);
strcpy(word1,word);
if(strcmp(word1,strrev(word))==0)
printf("%s is a palindrome",word1);
else
printf("%s is not a palindrome",word);
return 0;
}
10.Write down the C program to enter the name of ‘n’ students and sort them in ascending
order.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char name[30] [25], temp[30];
int i,j,n;
printf("Enter a number of student:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the name of students%d:\n",i+1);
scanf("%s",name[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("The sorted names are:\n");
for(i=0;i<n;i++)
{
printf("%s\n",name[i]);
}
return 0;
}