In C, how do i insert an integer into a string?

后端 未结 2 1493
春和景丽
春和景丽 2021-01-26 09:44

My code gets a string of characters. For example \"aaabbffffdd\" A function inserts to a new string the letters and the amount of times they appear. So the output for this specifi

2条回答
  •  囚心锁ツ
    2021-01-26 09:47

    You are right, the problem is the line:

    shorttxt[j + 1] = count;
    

    Change it to:

    shorttxt[j + 1] = count + '0';
    

    And you should be fine.

    The reason is that you don't want the number itself in the string, but the character representing the number. Adding the ascii value for the character 0 to the actual number gives you the correct result.

提交回复
热议问题