Home » » String Function in C Programming with Example

String Function in C Programming with Example

String Function in C Programming with Example

String Function in C Programming with Example

It has seven types
     1. String concatenation - It is used to join the one string with the another string - strcat
     2. String comparison - It is used to compare the one string with the another string - strcmp

    3. String copy - It is used to copy the one string with another string - strcp

     4. String length - It is used to find the length of the string - strlen

     5. String reverse - It is used to find the string in the reverse order - strrev

     6. String upper - It is used to find the string in the uppercase - strupr

     7. String lower - It is used to find the string in the lowercase - strlwr


String concatenation
Program:


#include
int main()
{
char string1[20];
char string2[20];
printf("Enter the string 1 is :");
scanf("%s",&string1);
printf("Enter the string 2 is:");
scanf("%s",&string2);
strcat(string1,string2);
printf("The given string 1 is %s",string1);
return 0;
}

Output :

Enter the string 1 is :sam
Enter the string 2 is:kumar
The given string 1 is samkumar

String length
Program :

#include

int main()
{
int x;
char String1 [20];
char String2 [20];
printf("Enter the string1 is :");
scanf("%s",&String1);
printf("Enter the string2 is :");
scanf("%s",&String2);
x=strcmp(String1,String2);
if(x==0)
printf("The given name is same");
else
printf("The given name is different");

return 0;
}

Output :

Enter the string1 is :sam
Enter the string2 is :kumar
The given name is different

String copy
Program :

#include

int main()
{
char string1 [20];
char string2 [20];
printf("Enter the string 1 is:");
scanf("%s",&string1);
printf("Enter the string 2 is:");
scanf("%s",&string2);
strcpy(string1, string2);
printf("\n the given string1 is %s",string1);
printf("\n the given string2 is %s",string2);

return 0;
}

Ouput :

Enter the string 1 is:sam
Enter the string 2 is:kumar

the given string1 is kumar
the given string2 is kumar

 If String  Length
Program :

#include

int main()
{
int n1,n2;
char string1 [20];
char string2 [20];
printf("Enter the string 1 is:");
scanf("%s",&string1);
printf("Enter the string 2 is:");
scanf("%s",&string2);
n1=strlen(string1);
n2-strlen(string2);
if(n1==n2)
printf("\nThe given length of the given string is same");
else
printf("\nThe given length of the given string is different");
return 0;
}

Output :

Enter the string 1 is:sam
Enter the string 2 is:kumar

The given length of the given string is different

Without int
Program :

#include

int main()
{
char string1 [20];
char string2 [20];
printf("Enter the string1 is:");
scanf("%s",&string1);
printf("Enter the string2 is:");
scanf("%s",&string2);
printf("\n the given length of the given string1 is %d",strlen(string1));
printf("\n the given length of the given string2 is %d",strlen(string2));

return 0;
}

Output :

Enter the string1 is:sam
Enter the string2 is:kumar

the given length of the given string1 is 3
the given length of the given string2 is 5

String reverse
Program :

#include

int main()
{
char string1 [20];
char string2 [20];
printf("Enter the string1 is:");
scanf("%s",&string1);
printf("Enter the string2 is :");
scanf("%s",&string2);
strrev(string1);
strrev(string2);
printf("\n The given reverse of the given string1 is :%s",string1);
printf("\n The given reverse of the given string2 is:%s",string2);
return 0;
}

Output :

Enter the string1: sam
Enter the string2: kumar

The given reverse of the given string1 is :mas
The given reverse of the given string2 is :ramuk

Upper lower
Program :

#include

int main()
{
char string1 [20];
char string2 [20];
printf("Enter the string1 is :");
scanf("%s",&string1);
printf("Enter the string2 is:");
scanf("%s",&string2);
strupr(string1);
strlwr(string2);
printf("\n The given reverse of the given string1 is %s",string1);
printf("\n The given reverse of the given string2 is %s",string2);

return 0;
}

String concordant
Program :

#include

int main()
{
int x;
char string1 [20];
char string2 [20];
printf("Enter the string1");
scanf("%s",&string1);
printf("Enter the string2");
scanf("%s",&string22);
strncat(string1,string2);
printf("The given string1 is %s",string1);
return 0;
}

String ncopy
Program :

#include

int main()
{
char string1 [20];
char string2 [20];
printf("Enter the string1");
scanf("%s",&string1);
printf("Enter the string2");
scanf("%s",&string2);
strncpy(string1,string2,4);
printf("\n The given name is %s",string1);
printf("\n The given name is %s",string2);
return 0;
}

Output :

Enter the string1sam
Enter the string2kumar

The given name is kuma
The given name is kumar

0 coment�rios:

Post a Comment

Link you don't want to vouch for Read more: https://html.com/seo/#ixzz5zVltiZ5g