Jul 2, 2013 at 12:30am UTC
Don't worry, I came up with a remarkably shorter one myself :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
char hittaStorLitenBokstav(char test){
char s;
char alphabet1[26] = "abcdefghjklmnopqrstuvwxyz" ;
char alphabet2[26] = "ABCDEFGHJKLMNOPQRSTUVWXYZ" ;
for (int x=0;x<26;x++){
if (test == alphabet1[x]){
s = alphabet2[x];
}
else if (test == alphabet2[x]){
s = alphabet1[x];
}
}
return s;
}
Last edited on Jul 2, 2013 at 12:31am UTC
Jul 2, 2013 at 12:59am UTC
Another simple way to do it using for loops.
Code
1 2 3 4 5 6 7
char hittaStorLitenBokstav(char test)
{
for (int jjj = 0; jjj < 26; ++jjj)
if (test == 'a' + jjj)
test = 'A' + jjj;
return test;
}
Explanation
Loop will run 26 times. Each time adding 1 to jjj starting at zero and ending at 25.
'a' + 0 == 'a'
'A' + 0 == 'A'
'a' + 1 == 'b'
'A' + 1 == 'B'
'a' + 25 == 'z'
'A' + 25 == 'Z'
Read
http://www.cplusplus.com/doc/ascii/
http://www.cplusplus.com/doc/tutorial/control/
Last edited on Jul 2, 2013 at 1:03am UTC
Jul 2, 2013 at 1:00am UTC
And another way is to use the toupper function - not sure whether you are allowed to use this, or were unaware of it.
This is in addition to
Disch 's post which is great for showing the OP how to make use of the char values.
HTH
Edit: there are 2 other versions of this function - which you can find in the reference. The 1 quoted is the C version. Here is a C++ one :
Last edited on Jul 2, 2013 at 1:03am UTC
Jul 2, 2013 at 7:53am UTC
@ Disch
I don't really get how your function works, but it certainly does.
@ TheIdeasMan
Actually I didn't know about it :)
Jul 2, 2013 at 8:24am UTC
@goran
@ Disch
I don't really get how your function works, but it certainly does
it works because each character has a value associated with it, so 'a' = 97, 'b' = 98, and so on.
capitals have values with them as well, 'A' = 65, 'B' = 66, and so on.
his code checks whether the input is a capital or not and then uses the values of the characters to do the conversion
so "C" for example
'C' - 'A' + 'a' = 'c' . . . which is the same as 67 - 65 + 97 = 99 or 'c'
i hope this helps explain whats going on there :D
1 2 3
char test = 'a' ;
int teste = test;
std::cout << teste;
you can use that to look at character values if you want
EDIT:spelling
Last edited on Jul 2, 2013 at 8:25am UTC
Jul 2, 2013 at 8:27am UTC
Now I get it :) Thank you for your explanation.