0% found this document useful (0 votes)
15 views8 pages

Program9 String Operations

The document outlines a program that implements string operations including length calculation, comparison, and concatenation using functions in C. It provides an algorithm and flowchart to demonstrate the process, along with the complete C code for execution. The program prompts the user to input two strings, calculates their lengths, compares them, and concatenates them, displaying the results accordingly.

Uploaded by

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

Program9 String Operations

The document outlines a program that implements string operations including length calculation, comparison, and concatenation using functions in C. It provides an algorithm and flowchart to demonstrate the process, along with the complete C code for execution. The program prompts the user to input two strings, calculates their lengths, compares them, and concatenates them, displaying the results accordingly.

Uploaded by

nayana_y
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROGRAM 9-

Write functions to implement string operations such as compare, concatenate, and find
string length. Use the parameter passing techniques.

ALGORITHM-

Step 1 : Start

Step 2 : Read str1, str2

Step 3:len1 = length(str1) and len2 = length(str2)

Step 4: Print len1, len2

Step 5: Compare(str1,str2)

Step 6: Concat(str1, str2)

Step 7: Stop

function length(str1)
for i←0 to str1[i]!= ‘\0’ do

count←count+1

end for

return count

function compare(str1,str2) len1←length(str1)


len2←length(str2)

if(len1!=len2) then

Print “Strings are Different”

end if
for i←0 to str1[i]!= ‘\0’ do

if(str1[i]!=str2[i]) then

Print strings are different.


end if

end for

else

Print “Strings are Same”

end if

function concat(str1,str2)

i=length(str1)

str1[i] ←str2[j]
for j0 to str2[j]!= ‘\0’ do str1[i] str2[j]
=i+1

end for

print str1
FLOWCHART-
PROGRAM-
#include<stdio.h>
#include<stdlib.h>

int length(char s[100]);


void compare(char str1[100],char str2[100]);
void concat(char str1[100],char str2[100]);

int length(char s[100])


{
int i,count=0;
for(i=0;s[i]!='\0';i++)
{
count ++;
}
return count;
}

void compare(char str1[100],char str2[100])


{
int i, flag=1;
if(length(str1)!=length(str2))
flag=0;
else
{
for(i=0;str1[i]!='\0';i++)
{
if(str1[i]!=str2[i])
{
flag=0;
break;
}
}

}
if(flag==1)
printf(“Strings are equal”):
else
printf(“Strings are different”);
}

void concat(char str1[100],char str2[100])


{
int i,j;
i=length(str1);
for(j=0;str2[j]!='\0';j++)
{
str1[i]=str2[j];
i++;
}
printf("The concatenated strings is %s\n",str1);
}

void main()
{
char str1[100],str2[100];
int len1,len2;
printf("Enter the 1st string\n");
scanf("%s",str1);
printf("Enter the 2nd string\n");
scanf("%s",str2);
len1=length(str1);
len2=length(str2);
printf("The length of first string is %d\n",len1);
printf("The length of second string is %d\n",len2);
compare(str1,str2);
concat(str1,str2);
}

You might also like