Change to numbers

One day, Jojo wants to send a secret message to his friend. Jojo has a sentence consists of alphabet and whitespace characters. Jojo wants to change all the alphabets into capital letter, then change some alphabets into numbers.
I = 1, R = 2, E = 3, A = 4, S = 5, G = 6, T = 7, B = 8, P = 9, O = 0
Help Jojo to change his sentence.
Format Input :
Input consists of 1 sentence S in one line. The sentence only contains alphanumeric character and whitespace.
Format Output :
Output one line of sentence, the conversion result from the initial sentence.
Constraints :
1 ≤|S|≤ 10.000

Sample Input :
Hello Jojo
Sample Output :
H3LL0 J0J0
1
2
3
4
Get input string.
Examine each char in turn - perhaps using a loop.
  Change each letter to capital
  Then, if the letter is one of the special letters, change it the to appropriate number
Last edited on
okay, i've change each letter to capital

#include <stdio.h>
#include <string.h>

int main()
{
char words[100];
int i;

gets(words);

for (i = 0; words[i]!='\0'; i++) {
if(words[i] >= 'a' && words[i] <= 'z') {
words[i] = words[i] - 32;
}
}
printf("%s", words);

return 0;
}
to change the special letters to appropriate number, the if statement is after the words[i] = words[i] - 32 ??
Yes.

I would note that this being C++, you could do yourself a favour and use string rather than an array, getline rather than gets, cout rather than printf. At the moment, you're writing C code.
Yes I know it, I look for C forum but I couldn't find it. So, I use this forum to ask because C is related to C++.
we do not mind to help C (esp simple questions), but we get a lot of people mixing C into C++ code and try to help them stop doing that :)

this problem begs a lookup table. (which many problems do. they are fast and easy to code).
make a nice char array of 256 entries. set it to 0-255 to start (its the ascii table, to start)
then manually correct it:
for(int x = 'a'; x <= 'z'; x++)
table[x] = 'A'+(x-'a'); //or if you have toupper or whatever use it here.
then again correct the others:
table['I'] = '1'; //etc

and then its easy:
for each letter in the string:
outstring[letter] = table[inputstring[letter]];
Last edited on
Topic archived. No new replies allowed.