Character array

Hello, I'm trying to get a word/sentance as input, save each character into an array, then output it backwards.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;

int main()
{	
	char word[255];
	int count = 0;
	
	cout >> "enter a word: ";
	while(cin.get(word))
	{
		count++;
	}
	
	while(count > 0)
	{
		cout >> word[count];
		count--;
	}
}


That's the idea of what I'm getting at, we haven't learned most of this stuff yet in class, just doing this for fun and practice.
Last edited on
here is the way I would do it. There are probably much better/efficient ways, but I am still a beginner myself, so its the best I can come up with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include<iostream>
using namespace std;

int main()
{	
	char word[255];
	int count = 0;
	
	cout << "enter a word: ";
	gets(word);
	
	cout << '\n';
	count = strlen(word)-1;
	for(int b=0; b<=count;b++) cout << word[b];
	
	cout << '\n';
	
	for(int a=count; a>=0; a--) cout << word[a];
	
	return 0;
}






Now is the time for all good men to come to the aid of their country.
.yrtnuoc rieht fo dia eht ot emoc ot nem doog lla rof emit eht si woN


Last edited on
ahhhh I see what you did thur... Yea i'm sure there's more efficient ways, but as far as my knowledge goes, that's what I was looking for. Gotta brush up on for loops.

thanks much!
Topic archived. No new replies allowed.