Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Incrementing Function in C

Tags:

c

c-strings

For my programming class, I am trying to write a function incrementstring() that takes the string 'str' passed in from a driver, and adds one to them. It should work with both letters and numbers (ex. '1' goes to '2', 'a' goes to 'b', 'z' goes to 'aa', 'ZZ' goes to 'AAA'). I have almost every test condition working, except for one bug that I can't seem to find a way around.

This is what I currently have:

void incrementstring(char* str){
int i;
int j;
int length = strlen(str);
for(i = strlen(str)-1; i >= 0; i--){
    if (str[i] == '9'){
            str[i] = '0';
            if (str[0] == '0'){
                    for (j = strlen(str)-1; j>=0; j--){ //This loop is the problem
                            str[j+1] = str[j];
                            }
                    str[0] = '1';
                    }
    }
    else if (str[i] == 'z'){
            if (str[0] == 'z'){
                    str[i] = 'a';
                    str[i+1] = 'a';
                    }
            str[i] = 'a';
            }

    else if (str[i] == 'Z'){
            if(str[0] == 'Z'){
                    str[i] = 'A';
                    str[i+1] = 'A';
            }
            str[i] = 'a';
    }
    else{
            str[i]++;
            return;
    }

}
}

When I run the function, this is what the driver outputs:

 1. testing "1"... = 2. Correct!
 2. testing "99"... = 100. Correct!
 3. testing "a"... = b. Correct!
 4. testing "d"... = e. Correct!
 5. testing "z"... = INCORRECT: we got "aa0". We should be getting "aa" instead.
 6. testing "aa"... = ab. Correct!
 7. testing "Az"... = Ba. Correct!
 8. testing "zz"... = aaa. Correct!
 9. testing "cw"... = cx. Correct!
 10. testing "tab"... = tac. Correct!
 11. testing "500"... = 501. Correct!

11 tests run.

I wrote a for loop in line 9 to handle the '99' to '100' condition. It takes every index of the string and shifts it one to the right, and then adds a '1' to the beginning of the string. However, this loop for some reason messes up the 5th test condition, as seen above. If I take the loop out, '99' will go to '00', but the 5th test will pass with no problems. I've hit a brick wall here and I was wondering if anybody can provide some insight.

I appreciate the help, thanks.

like image 219
acorbs Avatar asked Mar 27 '26 22:03

acorbs


1 Answers

While also keeping track of string length to make sure you do not overwrite its allocated space, add a null terminating character to each of your if() and if else segments:

str[0] = '1';
str[1] = 0; 

...

str[i] = 'a';
str[i+1] = 0;

And so on.

This final statement may not be doing what you expect it should do.
I believe what you want to do is to increment the expression to point to the next element of memory owned bystr.
Keep in mind that str is actually not an array. It is a pointer. The [...]
notation you are using is a convenience provided in C to allow array like referencing of pointers.
So, the expression str[i] for example can also be expressed as *(str + i). If it is the next memory location (where the next char is stored)you want, the expression would be: *(str + i++), which when using array notation translates to: str[i++]

Change the following from

else{
        str[i]++;

to:

else{
        str[i++]=0;
like image 62
ryyker Avatar answered Mar 31 '26 04:03

ryyker