C++ program to perform all operations on strings. Count the length of two strings, concatenate two strings, compare two strings.
#include"iostream.h"
#include"conio.h"
#include"stdio.h"
void findlength()
{
char str[30];
int l=0;
cout<<"\n Enter the string (size < =30) ";
gets(str);
while(str[l]!='\0')
{
l++;
}
cout<<"\n Length Of the given String is: "<< l<< endl;
}
void compare()
{
char str1[30], str2[30];
int l1=0,l2=0,i=0,flag=0;
cout<<"\n Enter the string1 (size< =30) ";
gets(str1);
while(str1[l1]!='\0')
{
l1++;
}
cout<<"\n Enter the string2 (size< =30) ";
gets(str2);
while(str2[l2]!='\0')
{
l2++;
}
if(l2!=l1)
{
cout<<"\n Strings are not Equal ";
}
else
{
for(i=0;i< l1; i++ )
{
if(str1[i]!=str2[i])
{
flag=1;
break;
}
}
if(flag==1)
{
cout<< "\n Strings are not Equal ";
}
else
{
cout<< "\n Strings are Equal ";
}
}
}
void concat()
{
char str1[30], str2[30];
int l1=0,l2=0,i=0,flag=0;
cout<<"\n Enter the string1 (size< =30 ) ";
gets(str1);
while(str1[l1]!='\0')
{
l1++;
}
cout<<"\n Enter the string2 (size< =30 ) ";
gets(str2);
while(str2[l2]!='\0')
{
l2++;
}
for(i=0;i < l2;i++)
{
str1[l1+i]=str2[i];
}
str1[l1+l2]='\0';
cout<<"\n The concatenated String is: ";
puts(str1);
}
void main()
{
clrscr();
cout<<" Enter your choice \n \t1.Find length of string\n\t"
"2.Compare two Strings \n\t3.Concatenate two strings\n\t4.Exit \n";
char ch;
cin>> ch;
do
{
if(ch=='1')
findlength();
if(ch=='2')
compare();
if(ch=='3')
concat();
cout<< "Enter your choice \n \t1.Find length of string\n\t"
"2.Compare two Strings \n\t3.Concatenate two strings\n\t4.Exit \n";
cin>> ch;
}while(ch!='4');
getch();
}
Step by Step explanation:
1.Header file "iostream.h" is used for including the function like cin and cout.
2.Header file "stdio.h" , "conio.h" for including the function like printf and scanf.
3."Findlenth" is a function of void type and it is used to calculate the lenth of string.
4.compare is a function of void type and it is used to compare the two string.
5.while is a loop it is used to apply the condition.
6.if is also a loop and it is too used for apply the condition.
7.else is a condition check statement it is used in case of failing the if statement.
8.Break is a keyword and it is used to break the loop.
9.concat is a function of void type and it is used to join the two string:string becomes in the form of matrix.
10.Main is also a function of void type and it works during the curley brackets.
Yes, this post, explain the concept very nicely...
ReplyDelete