You are on page 1of 6

10.

Write functions to implement string operations such as compare,


concatenate, string length. Convince the parameter passing techniques.

Algorithm:
Step1:[Start]
Begin
Step 2: [Read the input- two strings]
Read str1, str2
Step 3: [invoke the string length function with one parameter]
Len(str1)
[Invoke the string compare function with two parameters]
Comp(str1,str2);
[invoke the string concatenate function with two
parameters]
Concat(str1,str2);
Step 4: [Stop]
End
Function- Len()

Step1: [start]
begin
Step2: [Iterate through the string till the Null character and increment
the count]
Str1[length]!=’\0’
length++;
Step3:[Print the length]
Print length
Step4: [Stop]
End

Function- Comp()
Step1: [Start]
begin
Step2: [iterate through the string1 and string2 till they are equal]
Str1[i]==str2[i]
Step3: [till the end of the first string increment the counter]
If str1[i]==’\0’ then increment counter
Else
Break
Step 4: [find the difference between the characters]
Res = str1[i]-str2[i]
Step5: [check the value of the res to decide string are equal, greater or
lesser]
If res==0
Print strings are equal
Else if res<0
Print string1 is less than string 2
Else
Print string1 is greater than string2
Step6: [stop]
End

Function- concat()
Step 1: [Start]
Begin
Step 2: [iterate till the end of first string]
Str1[i]!=’\0’, Increment i
Step3: [iterate till the end of the second string and copy the characters
from string
2 to string 1]
str2[j]!='\0'
str1[i]=str2[j];
increment j
increment i
Step4: [Print the output]
Attach the null character to the end of first string
Print str1
Step5: [Stop]
End

Flowchart:
Program:
#include<stdio.h>
#include<string.h>
int length = 0,i=0,j=0,res; // Initialization
void len(char[]); //Function Declaration
void concat(char[],char[]);
void comp(char[],char[]);

void main()
{
char str1[100], str2[100];

printf("\nEnter the first String : ");


gets(str1);
printf("\nEnter the Second String : ");
gets(str2);

len(str1);
comp(str1,str2); //Function Calling
concat(str1,str2);
}

void len(char str1[]) //Function Defination


{
while (str1[length] != '\0')
length++;

printf("\nLength of the String1 is : %d", length);

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


{
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\n Concatenated String is %s\n",str1);
}

void comp(char str1[],char str2[])


{
int i=0;
while(str1[i]==str2[i])
{
if(str1[i]=='\0') break;
i++;
}
res=str1[i]-str2[i];

if(res==0)
printf("\n%s is equal to %s\n",str1,str2);
else if(res>0)
printf("\n%s is greater than %s \n",str1,str2);
else printf("\n %s is less than %s \n",str1,str2);
}

You might also like