0% found this document useful (0 votes)
23 views4 pages

Assign 3

assign

Uploaded by

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

Assign 3

assign

Uploaded by

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

# include <iostream>

#include<stdio.h>
# include <stdlib.h>

using namespace std;

int length(char a1[10]);


void copy(char a1[]);
int compare(char a1[],char a2[]);
void concat(char a1[],char a2[]);
void reverse(char a1[]);
void palindrome(char a1[]);
int substring(char a1[],char a2[]);

int main()
{

int a,ch,i,j,k,l;
char s1[10],s2[10],b[5];

cout<<"\nEnter the string:" ;


cin>>s1;

do
{

cout<<"\nEnter 1 for length of string";


cout<<"\nEnter 2 to copy string";
cout<<"\nEnter 3 for comparing 2 strings";
cout<<"\nEnter 4 for concatenation of 2 strings";
cout<<"\nEnter 5 for reverse of string";
cout<<"\nEnter 6 to find if the string is a palindrome";
cout<<"\nEnter 7 to find substring:";
cout<<"\nEnter your choice:";
cin>>ch;

switch(ch)
{
case 1:
i = length(s1);
cout<<"\n string length is = "<<i;
break;

case 2:
copy(s1);
break;

case 3:

cout<<"\nEnter compared string:";


cin>>s2;
j = compare(s1,s2);
if(j==1)
{
cout<<"\nStrings are same";
}
if(j==0)
{
cout<<"\nStrings are not same";
}
break;

case 4:
cout<<"\nEnter string:";
cin>>s2;
concat(s1,s2);
break;

case 5:
reverse(s1);
break;

case 6:
palindrome(s1);
break;

case 7:
cout<<"\nEnter substring:";
cin>>s2;
a = substring(s1,s2);
if(a==1)
{
cout<<"\n%s is a substring of %s"<<s2<<s1;
}
else
{
cout<<"\n%s is not a substring of %s"<<s2<<s1;
}
break;

default:
cout<<"\nDont you know your numbers?";
break;

}
cout<<"\n do you want to continue? y/n:";
cin>>b;
}
while(b[0]=='y');
return(0);
}

int length(char a1[10])


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

void copy(char a1[10])


{
int i;
char t1[10];
for(i=0;a1[i]!='\0';i++)
{
t1[i] = a1[i];
}
t1[i] = '\0';
cout<<"\nThe copied string is ="<<t1;
}

int compare(char a1[],char a2[])


{
int i=0,flag=0;
while(a1[i]!='\0'&&a2[i]!='\0')
{
if(a1[i]==a2[i])
flag=1;
if(a1[i]!=a2[i])
flag=0;
i++;
}
return flag;

void concat(char a1[],char a2[])


{
int i,j;
for(i=0;a1[i]!='\0';i++)
;
for(j=0;a2[j]!='\0';j++)
{
a1[i]=a2[j];
i++;
}
a1[i]='\0';
cout<<"\nConcatenated string is = "<<a1;
}

void reverse(char a1[])


{
int i,count=0;
char a2[10];
count=length(a1);
count--;
for(i=0;count>=0;count--)
{
a2[i]=a1[count];
i++;
}
a2[i]='\0';
cout<<"\n Reverse is = "<<a2;
}

void palindrome(char a1[])


{
int c,i,flag=0;
char a2[10];
c=length(a1);
c--;
for(i=0;a1[i]!='\0';i++)
{
a2[i] = a1[i];
}
a2[i] = '\0';

for(i=0;a2[i]!='\0';i++,c--)
{
if(a2[i]!=a1[c])
{
flag=1;
break;
}
}
if(flag==1)
{
cout<<"\n The string is not a palindrome";
}
else
{
cout<<"The string is a palindrome";
}
}

int substring(char a1[],char a2[])


{
int i,j,k,h=0,chk;
for(k=0;k<length(a1);k++)
{
chk=0;
if(a1[k]==a2[0])
{
j=k;
for(i=0;i<length(a2);i++)
{
if(a1[j]==a2[i])
{
chk++;
}
j++;
}
if(chk==length(a2))
{
h=1;
break;
}
}
}
return h;
}

You might also like