Strings In C++

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <algorithm>
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string>
using namespace 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)

Please Help Me.
Last edited on
str1_copy.replace(13,0,str2);

You are replacing with a string length 0.

cout<<str;
What is the str datatype?
You should be able to do this with std::string and char*, so make sure you have all the required headers.
Topic archived. No new replies allowed.