我有字符串「helLo,wORld!」我希望我的程序將其更改爲「Hello,World!」。我的程序工作正常,字符改變了,但我在感嘆號之後不斷收到額外的字符。我可能做錯了什麼?在字符串的結尾爲什麼我會在字符串末尾添加額外的字符?
void normalize_case(char str[], char result[])
{
if (islower(str[0]) == 1)
{
result[0] = toupper(str[0]);
}
for (int i = 1; str[i] != '\0'; i++)
{
if (isupper(str[i]) == 1)
{
result[i] = tolower(str[i]);
}
else if (islower(str[i]) == 1)
{
result[i] = str[i];
}
if (islower(str[i]) == 0 && isupper(str[i]) == 0)
{
result[i] = str[i];
}
if (str[i] == ' ')
{
result[i] = str[i];
}
if (str[i - 1] == ' ' && islower(str[i]) == 1)
{
result[i] = toupper(str[i]);
}
}
}
額外的人物像什麼? – Patashu
據我可以告訴你不是null終止'結果'。 –
這是混合了特殊字符,如問號和字母。我在想,可能是因爲我沒有在最後加上'\ 0'?我不記得如何做到這一點,但當我問我的導師時,他說在節目結束時加上'\ 0'......:/ – Karen