// operations on string
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
#include<process.h>
void main()
{
int i, j, len, choice, spaces, words,
caplet, pos, num, flag;
char str[100], sent[100], word2[50],
chara, opt;
int lenght (char[100]);
int space (char[100]);
int word (char[100]);
int capital(char[100]);
int palindrome(char[100]);
int linear_sreach(char[100], char);
int invers(char[100]);
int join(char [100], char [100]);
int sreach_word(char[100], char[100]);
do
{
clrscr();
cout<<"\t\t\t
Operations on string"
<<"\n\n MENU"
<<"\n 1. press 1
to find the number of characters in a string"
<<"\n 2. press 2
to find the number of spaces in the string"
<<"\n 3. press 3
to find the number of words in a string"
<<"\n 4. press 4
to find the number of capital letters"
<<"\n 5. press 5
to check if the string is a palindrome"
<<"\n 6. press 6
for linear search"
<<"\n 7. press 7
to replace small case letter with upper case letter and vise versa"
<<"\n 8. press 8
to join two sentence"
<<"\n 9. press 9
to find the occurrence of a word in a string"
<<"\n 10.press10
to exit";
cout<<"\n\n Enter
the string ";
gets(str);
cout<<"\n\n Enter
your choice";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\n\n
the number of characters in a string is :";
len=lenght(str);
cout<<len;
break;
case 2:
cout<<"\n\n
the number of spaces in the string is : ";
spaces=space(str);
cout<<spaces;
break;
case 3:
cout<<"\n\n
the number of words in a string is : ";
words=word(str);
cout<<words;
break;
case 4:
cout<<"the
number of capital letter is : ";
caplet=capital(str);
cout<<caplet;
break;
case 5:
flag=palindrome(str);
if(flag==0)
cout<<"\n
the string is a palindrome";
if
(flag==1)
cout<<"\n
the string is not a palindrome";
break;
case 6:
cout<<"\n
Enter the letter to be searched";
cin>>chara;
pos=linear_sreach(str,
chara);
if
(pos!=0)
cout<<"\n
the letter was found at the position"<<pos;
else
cout<<"\n
the letter was not present in the string";
break;
case 7:
invers(str);
break;
case 8:
cout<<"\n
enter the sentence that you need to join after the sentence";
gets(sent);
join(str,sent);
break;
case 9:
cout<<"\n
enter the number of letter in word that you needed to be searched";
cin>>num;
cout<<"\n
enter the word that need to be searched";
gets(word2);
sreach_word(str,
word2);
break;
case 10:
exit(0);
break;
default :
cout<<"\n
enter the number between 1-10 as per your choice we asked";
break;
}
cout<<"\n\n enter (y/n) if you want to continue the program
or not";
cin>>opt;
}while(opt=='y');
}
int lenght
(char str[100])
{
int i;
for (i=0;i<100;++i)
{
if(str[i]=='\0')
return (i);
}
return 0;
}
int space
(char str[100])
{
int i, space=0,len;
len=lenght(str);
for (i=0;i<len;++i)
{
if (str[i]==' ')
++space;
}
return (space);
}
int word
(char str[100])
{
int spaces, words;
spaces=space(str);
words=spaces+1;
return (words);
}
int
capital(char str[100])
{
int i, count=0, len;
len=lenght (str);
for(i=0;i<len;++i)
{
if(str[i]>='A' &&
str[i]<='Z')
++count;
}
return (count);
}
int
palindrome(char str[100] )
{
int i, flag=0, len, j;
len=lenght(str);
for(i=0;i<len;++i)
{
for(j=len;j<=0;++j)
{
if(str[i]!=str[j])
{
flag=1;
return
(flag);
}
}
}
return (flag);
}
int
linear_sreach(char str[100], char letter)
{
int i,len;
len=lenght(str);
for(i=0;i<len;++i)
{
if(str[i]==letter)
{
i=i+1;
return (i);
}
}
return 0;
}
int
invers(char str[100])
{
int i, len;
len=lenght(str);
for(i=0;i<len;++i)
{
if(str[i]>=65
&&str[i]<=92)
{
str[i]=str[i]+32;
}
else if(str[i]>=97
&& str[i]<=122)
{
str[i]=str[i]-32;
}
}
for(i=0;i<len;++i)
cout<<str[i];
return 0;
}
int
join(char str[100], char sent[100])
{
int len, slen, i, j;
char joinsent[200];
len=lenght(str);
slen=lenght(sent);
for(i=0;i<len;++i)
{
joinsent[i]=str[i];
}
for(j=0;j<slen;++j)
{
joinsent[i+j]=sent[j];
}
joinsent[i+j]='\0';
cout<<"the joined string is
"<<joinsent;
return 0;
}
int
sreach_word(char str[100], char word2[50])
{
int i, j, len, wlen, count=0, pos;
len=lenght(str);
wlen=lenght(word2);
for(i=0;i<len;++i)
{
for(j=0;j<wlen;++j)
{
if(str[i]==word2[j])
{
++count;
pos=i;
}
}
}
if(j==wlen)
cout<<"\n the word is present
in the sentence between "<<(pos+1-wlen)<<" position
to"<<pos+1<<" position";
return 0;
}
output
0 comments:
Post a Comment