Basically this function is supposed to accept a user inputted string, and the amount it is supposed to be transposed by 'x' amount (between 0-25).

The idea behind transposition is that if the user enters "abc" for the string and then wants to transpose it by 1. The new string is "bcd" changing the value of each character by 1. So if "zab" was transposed by 1, it'll be "abc".

However for some reason my function picks and chooses what goes in the if statement and I don't know why.

The idea behind my code is:

Let's take "zebra" and a transposition value of 25.

z = 122 on ASCII. 122+25 = 147 which is bigger than 122 so it should go in the if statement. Then the new value should ideally be 96 + the remainder of the transpose value (after it passes z).

However when I run my program the output = ôdaïz

ô = 147 on ascii table (skips if statement)
d = 100 (doesn't skip if statement)
a = 97 (doesn't skip if statement)
ï = 139 (skips if statement)
z = 122 (doesn't skip if statement)

So again.. I reiterate, why does my function pick and choose what goes in the if statement? What am I not seeing?

char* transposition(char* userInput, int transpose){
    int temp;
    for(int value = 0; value < strlen(userInput); value++){
        userInput[value] +=transpose;
        if(userInput[value] > 122){
            temp = userInput[value] - 122;
            userInput[value] = 96+temp;
    return (userInput);
4 Years
Discussion Span
Last Post by Carc369

The reason it's skipping is becuase char is actually a signed type, which means that it includes positive and negative numbers. This means that, when trying to interperet a char as an 8-bit integer, its effective value range is actually -128 to 127 instead of 0 to 255. When a character with an ASCII value higher than 127 goes into this if statement, your code is actually reading it as a negative value (147, for example, gets read as -108), which is always going to be less than 122.

To circumvent this behavior, all you need to do is typecast the character into an unsigned char, like so:

if((unsigned char)userInput[value] > 122)

Edited by Tumlee


Can you only enter alph characters such as 'a' to 'z', no caps or other special characters?

for(int value = 0; value < strlen(userInput); value++)

calling strlen() in a loop like that is poor programming practice because the return value never changes and its time consuming. Here is a better way to code that loop

for(int value = 0; userInput[value] != '\0'; value++){


Thanks man. I would've never figured this out. I appreciate it. Just learned something new.


That program will have a similar problem when the value of transpose is large enough to make an unsigned char overflow, such as 'z' is 122 decimal, so any value of transpose greater than (255 - 122) = 133 will cause data overflow.


Hey Ancient Dragon, sorry I missed your post. I was so excited that I just responded and continued on with my program. Just lowercase input, no special characters, and perfect input is assumed. There is no way the value can be greater than 255 because the user has to pick between 0-25. Thanks for your response on the NULL character. I didn't know that. Much appreciated.

You guys are very helpful.

Edited by Carc369

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.