Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Experiment No.

Aim:Implement all major functions of string.h in single C program using switch case to select
specific function from user choice (like strlen, strcat, strcpy, strcmp, strrev)

Code:
#include <stdio.h>

int main()
{
int ch;
printf("String functions\n1. strlen\n2. strcat\n3. strcpy\n4. strcmp\n5. strrev\n");
printf("Enter the function number from list to perform : ");
scanf("%d",&ch);
while ((getchar()) != '\n');
switch(ch){
case 1:{
char s1[100],c;
int l=0;
while((c=getchar())!='\n')
s1[l++] = c;
printf("Length of the string : %d",l);}
break;
case 2:{
char s1[100],c;
char s2[100];
int l=0,d=0;
while((c=getchar())!='\n')
s1[l++] = c;

while((c=getchar())!='\n')
s2[d++] = c;

for(int i=0;i<d;i++)
s1[l++] = s2[i];

printf("Concatenated string : ");


for(int i=0;i<l;i++)
printf("%c",s1[i]);
}
break;
case 3:{
char s1[100],c;
char s2[100];
int l=0,d=0;
while((c=getchar())!='\n')
s1[l++] = c;

while((c=getchar())!='\n')
s2[d++] = c;

for(int i=0;i<l;i++)
s2[i] = s1[i];

printf("String S1 : ");
for(int i=0;i<l;i++)
printf("%c",s1[i]);

printf("String S2 : ");
for(int i=0;i<l;i++)
printf("%c",s2[i]);
}
break;
case 4:{
char s1[100],c;
char s2[100];
int l=0,d=0;
while((c=getchar())!='\n')
s1[l++] = c;

while((c=getchar())!='\n')
s2[d++] = c;

int flag = 0;
if(l==d)
{
for(int i=0;i<l;i++)
if(s1[i]!=s2[i])
{flag=1;
break;
}
if(flag==1)
printf("String are dissimilar");
else
printf("Strings are similar");
}
else
printf("Strings are dissimilar.");}
break;
case 5:{
char s1[100],c;
char s2[100];
int l=0,d=0;
while((c=getchar())!='\n')
s1[l++] = c;

for(int i=0;i<l;i++)
s2[i] = s1[l-i-1];

printf("Reversed String : ");


for(int i=0;i<l;i++)
printf("%c",s2[i]);
}
break;
default:
printf("Unkown Option.");
break;}}

OUTPUT:

You might also like