I've been trying for hours to find out how to display an inputed word backwards but I just can't figure it out. Could someone help me please!! Thank you guys for your time.

if you mean for example ,
<input> hello
<output> olleh

then all you have to do is this :

string input;
cin >> input;

reverse(input.begin(),input.end());

cout << input << endl;

I tried that but the compiler is telling me that reverse identifier isn't found. Do i have to declare anything?

If you are not allowed to use the Standard Template Library (STL) a big hint is that you can treat a string as an array of characters.

EDIT: To your last question, you have to #include <algorithm> for that to work.

Edited 6 Years Ago by jonsca: n/a

not really getting the hint. i looked through the section on arrays but read nothing about displaying the contints backwards. I don't try something like cout<< *(line+4),*(line+3).... but get so many errors. I really don't understand how to read the errors either.

No, you don't wan tto use the reverse() method. You learn nothing by letting the compiler designers do your work for you.

1) Do you want to reverse the word into a separate string?
2) Or do you want to reverse the the string in place?
If (1), just loop through the string from the back to front and copy each character into a new string. (2) is just a little harder - you have to work from both ends at the same time.

3) Are you using string or a char array? The only real difference is the functions used: string.len() vs strlen() for example.

I did want to just reverse the letters of a five letter work in the same string. I just don't know how to right the alogrithm to make it work. I have the cin and the cout after but I want to know where to find it in the book but I just can't find it. Its frustrating.

ok post your code so i can see how your doing it.
btw try using a char array then invoking each index starting from the last first and the first last in a loop that exits after it prints the last character.
Example:

char input[8];
cout<<"input: ";
cin>>input;
for(x=7;x!=0,x--)
{
cout<<input[x];
}

NOTE: im not entirely sure that will work. let me check or you can try it.

Edited 6 Years Ago by Anarionist: n/a

No, you don't wan tto use the reverse() method. You learn nothing by letting the compiler designers do your work for you.

1) Do you want to reverse the word into a separate string?
2) Or do you want to reverse the the string in place?
If (1), just loop through the string from the back to front and copy each character into a new string. (2) is just a little harder - you have to work from both ends at the same time.

3) Are you using string or a char array? The only real difference is the functions used: string.len() vs strlen() for example.

just a quick question. there is a string array? or is the type itself just an array that is managed by a class? btw op pay no mind to this question you will confuse yourself.
EDIT: sorry for double post. :(

Edited 6 Years Ago by Anarionist: n/a

please don't laugh at my code I not very good at this. the last part is where i'm at. i can't think of a way to make the word that will be saved in line to display backwards. if someone enters five I want it to display evif. Just don't know how to get there.

//This program will utilize the String Length, Backward String, and the Word Count.
#include <iostream>
#include <string>
using namespace std;



int main()

{
	string city;  //to hold the city input
	string line; //to hold the line entered



	//Ask the user what city they were born in.
	cout<< " What city were you born in?\n";
	cout<<" ";cin>> city;

	//Tell the user how many letter are in the city they were born.

	cout<<" There are " << city.length() ;
	cout<< " letters in you city's name.\n";
	cout<<"\n";

	//Ask the user to enter a phrase
	cout<<" Please enter a word no more than 5 letters and I\n";
	cout<<" will display it backwards.\n";
	cout<<" ";cin>> line;

	//Display the line they entered backwards
	cout<<" Here is the word you entered backwards.\n";
	


	return 0;
}

Edited 6 Years Ago by WaltP: Oh, c'mon -- CODE tags, please. They are described all over the place!

char input[8];
cout<<"input: ";
cin>>input;
for(x=7;x!=0,x--)
{
cout<<input[x];
}

The above will work but it is limited becauseyou don't really know how large of a string the user will enter. Instead use this:

const int MAX_STR = 20;  // Obv can be any number
char input[MAX_STR];
cout<<"input: ";
cin>>input;
for(int x=strlen(input) - 1; x >= 0; x--)
	cout<<input[x];

Edited 6 Years Ago by hag++: forgot CODE

never be ashamed to ask for help if i had i wouldnt have been able to make this program im working on its only console based but i made it to where it can write multiple text files in one session. ill be right back going to test my theory with the char array. also you may want to check this out for an alternative to arrays if you use a lot Vectors at cplusplus

EDIT1:
oh thanks hag++ i never used the strlen() before. :P
also i only meant it as a guideline i normally would either code a variable of the length of say 5000 cause by the time the user inputs anything that large they are bored to tears. also if you can teach me anything about exception handling that would help me code better i would think.

Edited 6 Years Ago by Anarionist: n/a

the compiler gave me this error

error C2064: term does not evaluate to a function taking 1 arguments

What does that mean?

Dude, no one's going to laugh at you. Code tags would be nice, though. I'm going to echo a bit of what Walt P was saying. If you are saying that you want to display your string in reverse you don't need to store it like that you just have to output the characters in reverse.
What I mean is that in a string mystr = "hello" you can access h as mystr[0], e as mystr[1], etc. and you can find the length of a string by mystr.length() (it's= 5) where in both cases mystr is the name of your string. So armed with the above information write out on paper the indexes that you need to reverse the string "hello" based on its length (i.e. what index is o, what index is l, etc).


-------------------------
(Post 1000) - Ignore this

Edited 6 Years Ago by jonsca: n/a

the compiler gave me this error

error C2064: term does not evaluate to a function taking 1 arguments

What does that mean?

Whos code did you recieve that error with? my second example?

what if i don't know what the user will input? I did try to do it that way but it didn't work. I tried word +1,word +2,....

Ok I got it to compile finally but all I get when it is supposed to display backwards are funny symbols. this what I have.

//This program will utilize the String Length, Backward String, and the Word Count.
#include <iostream>
#include <string>
using namespace std;



int main()

{
	string city;  //to hold the city input
	const int MAX_STR= 20; //to hold the line entered
	char line[MAX_STR];


	//Ask the user what city they were born in.
	cout<< " What city were you born in?\n";
	cout<<" ";cin>> city;

	//Tell the user how many letter are in the city they were born.

	cout<<" There are " << city.length() ;
	cout<< " letters in you city's name.\n";
	cout<<"\n";

	//Ask the user to enter a phrase
	cout<<" Please enter a word no more than 5 letters and I\n";
	cout<<" will display it backwards.\n";
	cout<<" ";cin>> line;

	//Display the line they entered backwards
	cout<<" Here is the word you entered backwards.\n";
	cout<<line[5],line[4],line[3],line[2],line[1],line[0];

	


	return 0;
}

Edited 6 Years Ago by WaltP: CODE Tags! You have been warned....

You were trying to do it with pointers which is overcomplicating things. Just address it by word[0], word[1],word[2] etc. You don't have to know what the user will input if you get the string length using that function that I showed you earlier. mystr = "hello"; myst.length() is equal to 5, but remember that the index of 'o' is 4 is all. Can you generalize that?

Make line into a string, though (unless you have to use the char arrays but they are old and clunky)

just for now so you get the idea and get a successful compile make an array of say 100 so

#include <iostream>
using namespace std;

int main()
{
      char input[8];
      int x;
      cout<<"input: ";
      cin>>input;
      for(x=7;x>0;x--)
      {
      cout<<input[x]; 
//so this program takes the value of x and couts down by one each time the loop is gone through and it puts the value of the index that x matches out to the screen(console).
      }
      cin.get();
}

my example compiled fine for me but you may want to do what hag++ suggested which is making a safety for potential users like making sure they dont overflow the buffer.

Edited 6 Years Ago by Anarionist: n/a

I changed line into a string just now and got an error while in the dos screen when i was testing the program.

its and assertion error
and i got too pm's from a guy call walt saying that it have to do code tags what are those

jonsca couldnt he just do this

#include <iostream>
using namespace std;

int main()
{

      string input;
      int x;
      cout<<"input: ";
      cin>>input;
      for(x=input.length();x>0;x--)
      {
      cout<<input[x];
      }
      cin.get();
}

? yes i changed it around.

Edited 6 Years Ago by Anarionist: n/a

Are you using asserts? Something doesn't seem right there.

Also cout<<line[5],line[4],line[3],line[2],line[1],line[0]; is not correct, you need the << between each character.

For code tags it's easy, either type [code] //your code in the middle[/code] just like using html tags but with [] or you can highlight your code in the editor and click on the [code] button in the toolbar.

EDIT: I goofed on my noparse

Edited 3 Years Ago by happygeek: fixed formatting

just a quick question. there is a string array? or is the type itself just an array that is managed by a class?

You can have a string array -- but no there is not really a 'string array'. You are correct in it's an array managed by a class. string EmployeeName[100]; is a string array.

EDIT: sorry for double post. :(

Double posts are fine if you have something to add....

please don't laugh at my code I not very good at this.

there's nothing to laugh at. It doesn't do anything...

I changed line into a string just now and got an error while in the dos screen when i was testing the program.

Yes, always explain in full what's happening. We aren't psychic. If you don't give us enough info, you just may get the response "you must have done something wrong then" and that's it.... :icon_wink:


And don't forget the code tags!!! They're only explained:
1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled Read Me: Read This Before Posting
5) any place CODE tags were used, even in responses to your posts
6) Even on the background of the box you actually typed your message in!

got this error when I put in that code

error C2064: term does not evaluate to a function taking 1 arguments

jonsca couldnt he just do this
? yes i changed it around.

We were getting there but yes, except it's input.length()-1 for the starting index. Try not to give too much away so the OP can learn...

Edited 6 Years Ago by jonsca: n/a

got this error when I put in that code

error C2064: term does not evaluate to a function taking 1 arguments

Psychic???? :icon_wink: What's "that code"?

jonsca couldnt he just do this

#include <iostream>
using namespace std;

int main()
{

      string input;
      int x;
      cout<<"input: ";
      cin>>input;
      for(x=input.length();x>0;x--)
      {
      cout<<input[x];
      }
      cin.get();
}

? yes i changed it around.

Is there a specific reason that you are using a string instead of char array? It would be easiest with an array because of how you can manipulate it

Edited 6 Years Ago by hag++: n/a

This article has been dead for over six months. Start a new discussion instead.