C中的指针和数组

use*_*774 2 c arrays pointers

我是C和编程的新手.我被困在做作业练习.我的输出只显示大写的第一个字符,以及一些奇怪的数字中的以下字符.有人可以看看我的代码并给我一些关于我做错了什么以及解决问题的方法的提示吗?非常感谢您的帮助!

"写一个函数void sticky(char*word),其中word是单个单词,例如"sticky"或"RANDOM".stand()应修改单词以"sticky caps"出现(http://en.wikipedia. org/wiki/StudlyCaps),也就是说,字母必须是交替的情况(上部和下部),从第一个字母的大写字母开始.例如,"sticky"变为"StIcKy","RANDOM"变为"RaNdOm"注意字符串的结尾,用'\ 0'表示.你可以假设合法字符串被赋予sticky()函数."

#include <stdio.h>
#include <stdlib.h>

/*converts ch to upper case, assuming it is in lower case currently*/
char toUpperCase(char ch)
{
 return ch-'a'+'A';
}

/*converts ch to lower case, assuming it is in upper case currently*/
char toLowerCase(char ch)
{
 return ch-'A'+'a';
}

void sticky(char* word){
 /*Convert to sticky caps*/

for (int i = 0; i < sizeof(word); i++)
{
    if (i % 2 == 0)
    {
        word[i] = toUpperCase(word[i]);
    }
    else
    {
        word[i] = toLowerCase(word[i]);
    }
}
Run Code Online (Sandbox Code Playgroud)

}

int main(){
/*Read word from the keyboard using scanf*/
char word[256];
char *input;
input = word;
printf("Please enter a word:\n");
scanf("%s", input);

/*Call sticky*/
sticky(input);

/*Print the new word*/
printf("%s", input);

for (int i = 0; i < sizeof(input); i++)
{
    if (input[i] == '\n')
    {
        input[i] = '\0';
        break;
    }
}

return 0;
Run Code Online (Sandbox Code Playgroud)

}

Kei*_*las 5

你需要使用strlennot sizeof来查找char*字符串的长度