C Program To Manipulate 4 String Functions

C language provides a wide range of inbuilt function, to manipulate on string, by using this functions, We can..

    • Calculate length of string (strlen)
    • Copy one string to another string (strcpy)
    • Compare two strings (strcmp)
    • Concatenation of two strings

Bellow is the C program which will manipulate above 4 functions on string.

 

#include<stdio.h>
#include<string.h>
#include<process.h>
#include<conio.h>

void main()
{

char s[20],ss[20];
int n=6,l,i,j;

printf(“\n___________________________________________________\n”);
printf(“—————– Options —————-\n”);
printf(“1.Display length of string (strlen).\n”);
printf(“2.Make copy of enterd string into new string (strcpy).\n”);
printf(“3.Compare two strings (strcmp).\n”);
printf(“4. Concatenation of two strings (strcat).\n”);
printf(“5.Exit.\n”);
printf(“\n———————————————\n”);
printf(“Select proper option from above menu bar: \n”);

for(i=0;n>5;i++)
{
scanf(“%d”,&n);
if(n>5)
printf(“\nYou have selected invalid option!\nKeep attention and select proper option: “);
}

if(n==5)
{exit(0);}
else
printf(“\nEnter first string: “);
scanf(“%s”,&s);
if(n==1)
{
l=strlen(s);
printf(“\n—————————\n”);
printf(“Length of enterd string : %d”,l);
}
if(n==2)
{
strcpy(s,ss);
printf(“\nWe makes the copy of enterd string into new string!”);
printf(“\nNew string: %s”,ss);
}
if(n==3)
{
printf(“\nEnter second string to compare with first: “);
scanf(“%s”,&ss);

if(strcmp(s,ss)==0)
printf(“\nStrings are equal.”);
else
printf(“\nStrings are NOT equal.”);
}
if(n==4)
{
printf(“\nEnter second string to make concatnation: “);
scanf(“%s”,&ss);
printf(“\nWe makes the concated strings!”);
printf(“\nConcated string is: “);
puts(strcat(s,ss));
}
getch();

}

Thanks for your comment..