ASCII Character code advancement, in a char array

Well, i've been trying to right a little program, that will go through all of the characters in a string (or in this case a char array) and advance their ASCII character code by so much, essentially a cipher.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
char charstring[256];

cout<<"Please input a 10 character long string...\n";
cin>>charstring;



for (int i = 0; i <= 256; i++) {

}



system("PAUSE");
return 0;
}


what can i do so that during the for loop, i can select the individual character (charstring[i]) and add or subtract from its ASCII number?

thanks,
-Pb
Just use charstring[i] in your for loop. For example:

charstring[i] = charstring[i] + 1; // to add
alright, got another question, is there a command, that will return the character of an ASCII value? Like Chr$(156) (from VB6.0)

and is there a way to turn a character into a ASCII value?

thanks
-Pb
Are you looking to return the ASCII character from the ASCII character code, something like this?

cout << static_cast<char>(asciiNumber);

and is there a way to turn a character into a ASCII value?

I'm not quite sure I understand this statement. If you declare say:
char characterOne; retrieving the ASCII Character Code (number) or the Character symbol is dependent on how you use the variable.
This is a common misconception.

A char is not really a character. It's just an integer that happens to be interpreted as a character. For example, I can do this: char a=('A'+'6')/3; That's perfectly legal code.
The only moment when it matters whether something is a char or not is when printing with streams:
1
2
char a='A';
std::cout <<a;
That prints 'A' because the compiler automatically selects the overload of operator<< that better fits the situation. operator<<(char) happens to print character, not numbers.
If you want to print the actual value, you'd do this:
1
2
char a='A';
std::cout <<(int)a;

Note that that only applies to streams. If you do this:
1
2
char a='A';
printf("%d",a);
That will really print the value 65 (or whatever the encoding says). Even if a was declared as an int the result would be the same.
Last edited on
Alright thanks helios thats what i was looking for, albeit i have one more question :)

if i have an array of characters, but only the first 15 or so are used, what can i do to trim off the NULL characters on the end?

also, where can i download all of the "strings.h" files? I seem to be missing a "sys/cdef.h"

thanks,
-Pb
if i have an array of characters, but only the first 15 or so are used, what can i do to trim off the NULL characters on the end?
Nothing. The only way is to dynamically allocate a second array and copy the used characters to that array.

also, where can i download all of the "strings.h" files? I seem to be missing a "sys/cdef.h"
Nowhere. Reinstall the compiler.
Last edited on
Topic archived. No new replies allowed.