C
Programming/String manipulation
Commonly-used string functions are
WAP
to find length of a string.
#include <stdio.h>
#include <string.h>
void main()
{
char
str1[20] = "Welcome";
printf("Length of
string str1: %d", strlen(str1));
}
WAP to concatenate two strings.
#include <stdio.h>
#include <string.h>
void main()
{
char
str1[10] = "Wel";
char
str2[10] = "Come";
printf("Concatenate of two string is : %s", strcat(str1,str2));
}
WAP to
Sort strings in alphabetical order
#include<stdio.h>
#include<string.h>
void main(){
int i,j,count;
char str[25][25],temp[25];
puts("How
many strings u are going to enter?: ");
scanf("%d",&count);
OUTPUT How many strings u are going to enter?:
3
Enter Strings one by one:
Ram
Ajay
Sanjay
Order of Sorted Strings:
Ajay
Ram
Sanjay
puts("Enter Strings one by one:
");
for(i=0;i<=count;i++)
gets(str[i]);
for(i=0;i<=count;i++)
for(j=i+1;j<=count;j++){
if(strcmp(str[i],str[j])>0){
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("Order of
Sorted Strings:");
for(i=0;i<=count;i++)
puts(str[i]);
}