简体   繁体   中英

String Incrementing Function in C

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.

Your issue is that you're not NULL-terminating your string in the driver program. Running your code with my own driver program works perfectly, so any additional help would require you to share your driver program with us.

All you have to do is, after you fill the char * with the string, make the next character a '\\0' character. Since the strlen function simply iterates over the array of char s until it reaches a NULL terminating character, you have to terminate all strings with that character before using them.

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 by str .
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;

它对“zaz”有效吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM