----------------------------------------------
3. Write a set of string manipulation functions e.g. for getting a sub-string from a given position,
Copying one string to another, Reversing a string, adding one string to another
----------------------------------------------
PROGRAM:-
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char string1[25],string2[25];
int choice;
char* p;
while (1)
{
printf("the list of string manipulation operations\n");
printf("__________________________________________\n");
printf("\n1.Finding string length\n2.Finding string concatenation\n3.Compare two strings\n4.Copying two strings\n5.Reverse of string\n6.Extract the given string\n7.Exit\n");
printf("_____________________________\n");
printf("choose (1-7) from the above list:");
scanf("%d",&choice);
switch (choice)
{
case 1:
printf("enter the string1 :");
scanf("%s",string1);
printf("the length of the string is %ld\n",strlen(string1));
printf("__________________________________________\n");
break;
case 2:
printf("enter two strings :");
scanf("%s%s",string1,string2);
strcat(string1,string2);
printf("the concatenated string is =%s\n",string1);
printf("__________________________________________\n");
break;
case 3:
printf("enter two strings :");
scanf("%s%s",string1,string2);
if (strcmp(string1,string2)==0)
{ printf("the given two strings are equal");}
else
{ printf("the given two strings are not equal\n");}
printf("__________________________________________\n");
break;
case 4:
printf("enter the string :");
scanf("%s",string1);
printf("string1=%s\n",string1);
printf("after copying string1 to string2\n");
strcpy(string2,string1);
printf("string2=%s\n",string2);
printf("__________________________________________\n");
break;
/* case 5:
printf("enter the string");
scanf("%s",string1);
printf("before reversing string1 = %s",string1);
strrev(string1);
printf("after reversing of string is = %s",string1);
printf("___________________________________\n");
break;*/
case 6:
printf("Extracting substring from a given string\n");
printf("enter the string :");
scanf("%s",string1);
printf("Enter the sub string to extract :");
scanf("%s",string2);
p = strstr(string1,string2);
if (p)
{
printf("string found\n");
printf("First occurence of string %s in %s is %s\n",string1,string1,p);
printf("__________________________________________\n");
}
else
{ printf("string not Found\n");
printf("__________________________________________\n");}
break;
case 7:
exit(0);
}
}
return 0;
}
output:-
selection of text is disabled so please dont try to copy and pasting the code
0 Comments