String of length zero

I was given an assignment in my C++ class to write a code to take an user inputted string and test whether or not the string inputted was a Palindrome or not...this part was easy. My problem is the last sentence of the problem given: "The main function must continue to read the next string until an empty string is entered (string with length 0)." I thought I had this figured out by just measuring string length and if the length ==0, the code would break, but that did now work. Every time 'enter' is hit while using Command Prompt, the cursor just continues onto the next line and doesn't return a string of size 0...so my question is, as a beginner, how does a user input a string of size zero and how does one test for it?

Thanks in advance!
You have to post the code that is doing the reading so we can see how you could break out of the loop.
don't quote me on this.

i think that in K&R "C" a string contains absolutely nothing when it is defined.
and that in C++ a string is defined with the null terminator " /0 "
so that it'll be difficult to get it empty.

However, you can check for the string's length = 1
and it's value .eq. to the null char "/0"

while it won't be empty, exactly, it also won't contain anything.

...
( is your glass empty? No, it's full of air. )


Last edited on
Sorry, didn't think that was needed...

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
#include <iostream>
#include <string>

using namespace std;

int InputString (string str);
bool PalindromeTest (string str, int n);
void PrintMessage (string str, bool ans);

string str;

int InputString (string str)
{
	int n=str.length();
	return n;
}

bool PalindromeTest (string str, int n)
{
	int x=n-1;
	for (int i=0; i<=x; i++)
	{
		if (str[i]==str [x-i])
			continue;
		else
			return false;
	}
	return true;
}

void PrintMessage (string str, bool ans)
{
	switch (ans)
	{
	case true:	 
			cout<<"String '"<<str<<"' is a Palindrome"<<endl;
			break;
	case false:
			cout<<"String '"<<str<<"' is NOT a Palindrome"<<endl;
			break;
	}
}

int main ()
{
	cout<<"Please Input a Word: ";
	cin>>str;
	PrintMessage(str, PalindromeTest(str, InputString(str)));
	
}
For what you are trying to do, I would use getline.
1
2
3
4
5
6
7
8
9
10
int main () {
  string str;
  cout<<"Please Input a Word: ";
  getline(cin,str);
  while(str.length()){
     PrintMessage(str, PalindromeTest(str, InputString(str)));
     cout<<"Please Input a Word: ";
     getline(cin,str);    
  }
}

$ ./a.out
Please Input a Word: ttt
String 'ttt' is a Palindrome
Please Input a Word: aba
String 'aba' is a Palindrome

The operator>> leaves the '\n' in the stream and when trying to get the next string operator>> will ignore all white space until characters are found. Not what you want.

Just beware that if someone enters "testing this thing", the value of str will be that whole string. So, if you want to protect against that you would have to test to see if the input string could be broken up and then call you function on each string.
Last edited on
Okay, thank you very much. I will start having to use getline() more now that I know what it does! Thanks again!
Topic archived. No new replies allowed.