Hey Freinds
I'm not new to programming but having some problems in C++ strings
I'm making a program to
1)Reverse String.
2)Check if Palindrome
3)Replacement of substring by another string.";
4)Counting Words,Lines,Characters in a string";
5)Sorting alphabetically.
So far i have been able to do 1,2 and 4 .The funtion i'm using for (3) is right but i'm not getting the desired output
Also, I've not been able to Implement (5).
Please Help me
#include <algorithm>
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string>
usingnamespace std;
int main()
{
int optn;
char ans='y';
string str1("RaTs LiVe On nO eViL sTaR");;
string str2("ThIs PlAnEt ");
string str1_rev;
int iSpace = 0;
int iLength,n,i;
string str1_copy(str1);
string::iterator it;
string::reverse_iterator reverse;
cout<<"\n\t\tProgram to Implement String Operations";
while (ans == 'y')
{
cout<<"\n\t---------------------Menu---------------------";
cout<<"\n\t1)Reverse String.\t2)Check if Palindrome";
cout<<"\n\t3)Replacement of substring by another string.";
cout<<"\n\t4)Counting Words,Lines,Characters in a string";
cout<<"\n\t5)Sorting alphabetically.\t0)Exit.";
cout<<"\n\t----------------------------------------------";
cout<<"\n\tChoose Option : ";
cin>>optn;
switch(optn)
{
case 0:
cout<<"\n\tExiting Program!";
getch();
exit(0);
break;
case 1:
cout<<"\b\tReverse String:";
for (reverse=str1.rbegin(); reverse<str1.rend(); reverse++)
cout<< *reverse;
break;
case 2:
for (reverse=str1.rbegin(); reverse<str1.rend();reverse++)
str1_rev += *reverse;
if(str1.compare (str1_rev)==0)
cout<< "\n\tThe given string is a palindrome";
else
cout<< "\n\tThe given string is not a palindrome.";
break;
case 3:
str1_copy.replace(13,0,str2); //Expected RaTs LiVe On ThIs PlAnEt
for(it=str1_copy.begin ();it<str1_copy.end ();it++)
cout<< *it;
break;
case 4:
iLength = str1_copy.length();
for(i = 0; i < iLength; i++)
{
if((str1_copy.at(i) == ' ') || (str1_copy.at(i) == '\t') || (str1_copy.at(i) =='\0'))
iSpace ++;
}
printf("\n\tNumber of characters : %d", (iLength - iSpace));
n = count(str1_copy.begin(), str1_copy.end(), ' ');
cout<<"\n\tThere are "<< n + 1 <<" Words";
break;
case 5:
break;
default:cout<<"\n\tInvalid Option!";
getch();
exit(0);
}
cout<<"\n\tContinue ?[y/n]:";
cin>>ans;
}
cout<<"\tPress any key to Exit.";
getch();
return 0;
}
Also I would like to know if it is possible to accept strings from user directly as it is done for arrays?
I tried
cout<<str;
to get the error..
1 2
error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is
no acceptable conversion)