0% found this document useful (0 votes)
17 views7 pages

PiC Ex - 5

The document contains 6 programs that demonstrate various string operations in C like concatenation, copying, comparing, counting length, finding number of words/lines, and reversing a string. The programs show how to perform these operations both with and without built-in string functions like strcat(), strcpy(), strlen(), strcmp() etc. They provide sample inputs and outputs for each program.

Uploaded by

gopika.26csa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

PiC Ex - 5

The document contains 6 programs that demonstrate various string operations in C like concatenation, copying, comparing, counting length, finding number of words/lines, and reversing a string. The programs show how to perform these operations both with and without built-in string functions like strcat(), strcpy(), strlen(), strcmp() etc. They provide sample inputs and outputs for each program.

Uploaded by

gopika.26csa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

EX:05 STRING OPERATIONS

1.program:
#include <stdio.h>
#include <string.h>
int main()
{
char a[10],b[10];
int i,j;
printf("enter string 1:");

16
scanf("%s",a);
printf("enter string 2:");
scanf("%s",b);
for(i=0;a[i]!='\0';i++)
{
S0
}
for(j=0;b[j]!='\0';j++)
{
a[i]=b[j];
i++;
}
C
a[i+1]='\0';
printf("after concatenation(without using built in function)%s",a);
printf("after concatenation(using built in function)%s",strcat(a,b));
22

return 0;
}
Output:
enter string 1:good
enter string 2:morning
after concatenation(without using built in function)goodmorning
after concatenation(using built in function)goodmorning
4.program:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[10],str2[10];
int i;
printf("enter string 1:");
scanf("%s",str1);
for(i=0;str1[i]!='\0';i++)

16
{
str2[i]=str1[i];
}
str2[i]='\0';
printf("copied string(without built in function):%s",str2);
S0
strcpy(str2,str1);
printf("copied string(with built in function):%s",str2);
return 0;

}
Output:
C
enter string 1:apple
copied string(without built in function):apple
copied string(with built in function):apple
22
3.program:
#include<stdio.h>
#include<string.h>
int main()
{
char a[10];
int l;
printf("enter string 1:");
scanf("%s",a);
l=strlen(a);

16
printf("length of the string(using built in function):%d",l);
int i,count=0;
for(i=0;a[i]!='\0';i++)
{
count++;
S0
}
printf("length of the string(without using built in function):%d",count);
return 0;

}
C
Output:
enter string 1:apple
length of the string(using built in function):5
22

length of the string(without using built in function):5


2.program:
#include <stdio.h>
#include <string.h>
int main()
{
char a[20],b[20];
int i;
printf("enter string 1:");
scanf("%s",a);
printf("enter string 2:");

16
scanf("%s",b);
for(i=0;a[i]!='\0';i++)
{
if(a[i]==b[i])
{
S0 continue;
}
else
{
printf("strings are different");
return 0;
C
}
22

}
if (b[i]=='\0')
{
printf("strings are same");
}
else
{
printf("strings are different");
}
return 0;
}
Output:
enter string 1:cat
enter string 2:dog
strings are different

enter string 1:doll


enter string 2:doll
strings are same

16
S0
C
22
5.program:

#include<stdio.h>
#include<string.h>
void main()
{
char str[10000];
int word=1,letter,line=1,i;

16
printf("Enter the String:\n");
scanf("%[^#]s",str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==' '||str[i]=='\n')
S0 word+=1;
if(str[i]=='\n')
line+=1;
}
C
letter=strlen(str)-word+1;
printf("There totaly %d letter, %d words and %d lines",letter,word,line);
}
22

Output:

Enter the String:


hi hello how are you?#
There totaly 17 letter, 5 words and 1 lines
6.program:

#include<stdio.h>
#include<string.h>
void main()
{
char str[20],i,j,temp,l,h;
printf("Enter String: ");
scanf("%s",str);
l=0;

16
h=strlen(str)-1;
while(l<=h)
{
while(!(str[l]>='a'&& str[l]<='z'||str[l]>='A'&& str[l]<='Z'))
{
S0 l++;
}

while(!(str[h]>='a'&& str[h]<='z'||str[h]>='A'&& str[h]<='Z'))


{
h--;
C
}
temp=str[l];
str[l]=str[h];
22

str[h]=temp;
l++;
h--;
}
printf("reversed: %s",str);
}

Output:

Enter String: hello@#$who%^are*&you?


reversed: uoyer@#$aoh%^wol*&leh?

You might also like