Invert AZ program

I am trying to make a program that will reverse the characters of the word i will give from A-Z to Z-A
example: if i give "abc" it will give "zyx"

My code is the following:


#include <iostream>
using namespace std;
int main (int argc, char * const argv[]) {


char array[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

char rname[20]; // Name of the word


int username;

for(username=0;username<=20;username++)
{

cin>> rname[username];

if (rname[username]==',') {
break;

}

}

char arraypos[26]; // to get the position of the character d=4
int po=0;
for (username=0; username<=26; username++) {

if (array[username]==rname[po]) {

arraypos[po]=username;
po=po+1;
}
}

int oa=0;
int usernamea=30;
char revarray[26];

for (username=26; username>=0; username--) {
oa=oa+1;
revarray[username]=array[26-oa];

}
int xo=0;

for (xo=0; xo<=20; xo++) {
cout << revarray[xo];
}

return 0;
}
Please use code tags around your code so it's easier for us to interpret.
Fill a two dimentional array example array[1][26]. Load array[0][26] with the alphabet and array[1][26] with the alphabit in reverse. Take each character from rname and scan through array[0][26] with a for() loop and an integer to find the position of the letter then take that number and use it in the second dimension of array[1][26]. The code might look something like this.
1
2
3
4
5
6
7
8
9
for(int i =0; i < 26; i++)
{
   if(User_Input == array[0][i])
    { 
       cout << array[1][i];
                                      }//End of If()
   
                                                             }//End of for()

Replace the cout with whatever it is you want to do of course. I didn't compile or test this, it's off the top of my head so YMMV. Enjoy.
Last edited on
No scanning necessary. Use a lookup table.
http://www.cplusplus.com/forum/beginner/31119/#msg168499
You don't need to store both the inversed and the normal alphabet. One will do. Just use some character arithmetic magic.

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

using namespace std;

int main()
{
    char rAlph[26] = {'z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'};
    string Name, Inv;
    cout << "Input your name: ";
    getline(cin, Name);
    for(unsigned u = 0; u<Name.size(); u++)
    {
        if (Name[u]>'a' && Name[u]<'z') Inv.push_back(rAlph[Name[u]-'a']);
        else if (Name[u]>'A' && Name[u]<'Z') Inv.push_back(rAlph[Name[u]-'A']);
        else {return 1;}
    }
    cout << Name << "\n" << Inv;
}

Above code uses an inversed alphabet, it can be done with a normal one, too.
You don't need to hard code the alphabet at all. Remember that 'a' < 'z' and that 'a' + 1 == 'b'.
I am still trying to make this prog work i changed it to the following code but it gived my an error on the first array "too many initializers for 'char"
my code:

I used now ricomoss dont know if this is what you asked for

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
#include <iostream>
using namespace std;
int main (int argc, char * const argv[]) {
   

	char array[1][26]= {  {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'},
						  {'z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'}   };


	char rname[20];
	int arraypos[20];
	
	
	int abc;
	
	for(abc=0;abc<=20;abc++)
	{
	
		cin>> rname[abc];
		
		
	if (rname[abc]==',') 
		{
		break;
		
		}
		arraypos[abc]=abc;
		
	}
	
	
	int io,yo,oi;
	
	for (io=0;io<=20;io++){
		
		if (arraypos[io]>=1) {
			yo=arraypos[io];
		}
		else {
			break;
		}

		
		for (oi=0; oi<=26; oi++) {
			if (yo=oi) {
				cout << "Your world is :" << array[2][yo];
			}
			

			
		}
	
		yo=0;
	
	
	}
	
	return 0;
}


Last yo=0 was meant to be in the for loop.
And cout<<"Your word is :"<< array[1][yo]
@filipe:
I know, but the purpose of this forum is to not post direct solutions, but help someone in the learning process. It would be good to implement that, but for the above stated reason, I wont. ;)
Topic archived. No new replies allowed.