Write functions to implement string operations such as compare, concatenate, string length.
Convince the
parameter passing techniques.
Algorithm:
Step 1: Start
Step 2: Read two strings S1 and S2
Step 3: Compute len_str(s1)
Step 4: Display the length of string s1
Step 5: Compute len_str(s2)
Step 6: Display the length of string s2
Step 7: Compare two strings using comp_str(s1,s2)
a. If the function returns 0 Display the strings are equal
b. If the function returns 1 Display the strings are not equal
Step 8: Concatenate two strings using concat_str(s1,s2)
Step 9: Stop
Algorithm for String length:
Step 1: Initialize len=0
Step 2: for (i = 0; s[i] != '\0'; i++)
Increment len
Step 3: Return len to the main program
Algorithm for String Comparison:
Step 1: Compute the length of string s1 and s2 using the function len_str(s1) and len_str(s2)
Step 2: if lengths are not equal
return 1
Step 3: for (i = 0; i < len1; i++)
if (s1[i] !=s2[i])
return 1
Step 4: return 0
Algorithm for String Concatenation:
Step 1: Initialize i=0 and j=length of string1 using len_str(s1)
Step 2: while(s2[i]!='\0')
Assign s1[j]=s2[i];
Increment j and i
Step 3: Assign s1[j]=null
Step 4: Display Concatenated String S1
Step 5: Return to main Program
Test Cases:
Sl No. Test Data Expected Output
String1 String2
Length of the string1=5
Length of the string2=5
1 Hello World Strings are not equal
Concatenation of string1 and string2
HelloWorld
Length of the string1=8
Length of the string2=7
2 Computer Science Strings are not equal
Concatenation of string1 and string2
ComputerScience
Length of the string1=8
Length of the string2=8
3 Computer Computer Strings are not equal
Concatenation of string1 and string2
ComputerComputer
Viva Questions:
[Link] Questions
1. What is the difference between string and character?
2. What is string?
3. What is the format specifier for the string?
4. What is string copy function?
5. Name the header file for string?
Modification Questions:
[Link] Questions
1. Write functions to implement string copy operations.
2. Write functions to reverse a string
3. Implement a program to check if a string is a palindrome or not
Flowchart:
start
Read str1,str2
Call cmp2str(str1,str2)
Call concat2str(str1,str2)
Call length(str1)
Call length(str2)
stop
cmp2str(str1,str2)
Concat2str(str1
, str2)
length(str)
Print F
Strcmp(str1,
both are
str2)=0 Res=strcat(str1,
not equal res=len(str)
str2)
T
Print both are
print res print res
equal
return return
return
Program:
#include <stdio.h>
#include<conio.h>
int len_str(char s[]);
int comp_str(char s1[], char s2[]) ;
void concat_str(char s1[],char s2[],char s3[]);
void main()
{
char s1[50],s2[50],s3[5],c;
int len-0;
clrscr();
printf("enter the 1st string\n");
scanf("%s",s1);
printf("enter the 2nd string\n");
scanf("%s",s2);
len=len_str(s1);
printf("length of the string1 = %d\n",len);
len=len_str(s2);
printf("length of the string2 = %d\n",len);
c=comp_str(s1,s2);
if(c==0)
printf("strings are equal\n");
else
printf("strings are not equal\n");
concat_str(s1,s2);
getch();
}
int len_str(char s[])
{
int len = 0, i;
for (i = 0; s[i] != '\0'; i++)
{
len++;
} return len;
}
int comp_str(char s1[], char s2[])
{
int len1, len2, i;
len1 = len_str(s1);
len2 = len_str(s2);
if (len1 != len2)
return 1;
for (i = 0; i < len1; i++)
{
if (s1[i] !=s2[i])
return 1;
} return 0;
}
void concat_str(char s1[],char s2[],char s3[])
{
int i,j;
for(i=0;i<len_str(s1);i++)
s3[i]=s1[i];
for(j=0;j<len_str(s2);j++)
s3[i++]=s2[j];
s1[j]='\0';
printf("concatination of string1 and string2 %s \n",s3);
}