Trying to reverse a given word in a different way

Ok, the thing is, I have no idea how to do it legitimately with strings, cin etc., so I did it in twisted way.. the problem is I have to enter the word twice, I have no idea why.
For example, I enter Name, press enter, then I have to enter the same or different name again and it will automatically press enter after there are the same amount of characters as in the first name.. so I would try to enter Cupcakes for the second time, but after fourth character, c, the loop will be stopped and it will give me cpuC as a result.

The thing is I don't want to type second time. If it's impossible to change that I would, at least, like program to say something like "Now enter the name second time, for fun". Thanks.

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
char j[1000];
int n=0;
while (cin.get() != '\n'){j[n]= _getch(); cout << j[n]; n++;}
while (n>=0) {cout << j[n]; n--;}
}
Last edited on
This code is invalid because element j[n] was not initialized and has undefined value.
I have no idea what you mean. Do you mean something like
1
2
int t=0;
while (t<=300) {j[n]=t; t++;}


?
If I understand correctly I think I should initialize j[n] with as much as elements as the word length is going to be.. but how should I know that? My loop does it for me by asking to type the word twice.
Last edited on
I mean that in the second loop there is no such initializrd element of the array for the initial value of n. Moreover it is a bad idea to use non-standard function _getch.
Last edited on
How come there isn't? n has value after the first loop (because value rises from zero). As far as I understand, If n value is for example y (which is the same number as there are characters in a word), then in the second loop initial j[n] value will be y too, which will go down to zero.
Well consider the first loop. Let assume that you entered only one element (character). In this case the following were done 1) j[0] was assigned a value; 2) n become equal to 1.

So what value does j[1] have that is used in the second loop?
Last edited on
oh.

so that's where that space is coming from..
but if I put in the program the following after the first loop:
n=n-1;

it would change nothing, only the space won't be displayed (program will look like that):
1
2
g
gg


while it looked like this before:

1
2
g
g g 


I'm sorry but that doesn't solve my question at all
Last edited on
As I said your program is invalid. You should at first write the program correctly without _getch and with correct access to elements of the array and only after that ask other questions.
The point of this program was to reverse the given word without using strings or something like that. There is no way to change this program to correct, the only one option is to write another program from the beginning which I can't do because I don't know how, that's why I created this invalid program in the first place
You can do this by means of a recursive function. You in fact already uses strings that is character arrays. So I do not understand the problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

int main()
{
   char s[1000];

   cin.getline( s, sizeof( s ) );

   const char *p = s + strlen( s );

   while ( p != s ) cout << *--p;
   cout << endl;
}
Last edited on
looks good, but I don't really know what that stuff means. thanks, I'll study it myself
Another example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
	const int N = 1000;
	char s[N];

	int i = 0;
	char c;
	while ( i < N && ( c = std::cin.get() ) != '\n' ) 
	{
		s[i++] = c;
	}

	while ( i != 0 ) std::cout << s[--i];
	std::cout << std::endl;
}
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
#include <iostream>
using namespace std;

template <class BN>
void _swap(BN &X, BN &Y)
{
    BN Z(X);
    X = Y;
    Y = Z;
}

int size (char *p)
{
    return (*p ? (1 + size(++p)) : 0);
}

void reverse(char * word, int len)
{
    for (int s = 0; s < len--; ++s)
        _swap(word[s], word[len]);
}

int main()
{
    char s[100];
    
    cin.getline(s, sizeof s);//get "string" from user
    
    reverse(s, size(s));
    
    cout  << s << endl;
    return 0;
}
Last edited on
Topic archived. No new replies allowed.