Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将s中字符串中每个单词的第一个字母大写_C - Fatal编程技术网

C 将s中字符串中每个单词的第一个字母大写

C 将s中字符串中每个单词的第一个字母大写,c,C,我想让一个单词的第一个字母大写,但我最终改变了整个字符串。任何帮助 { //Prompt for the name char *s = GetString(); /* some function that returns a string */ //capitalize for(int i = 0,n = strlen(s);i < n;i++) { printf("%c",toupper(s[0])); } printf("\n"

我想让一个单词的第一个字母大写,但我最终改变了整个字符串。任何帮助

{
   //Prompt for the name
   char *s  = GetString(); /* some function that returns a string */

   //capitalize
   for(int i = 0,n = strlen(s);i < n;i++)
   {
      printf("%c",toupper(s[0]));
   }
   printf("\n");
}

那么以下内容如何:

void
capitalise(char *s)
{
     int start = 1;
     for (; *s; s++)
     {
          if (start)
              *s = toupper(*s);
          start = isspace(*s);
     }
}
当您将s传递给strlen时,我假定它实际上是一个char*,并且该字符串是您没有告诉我们的某个奇怪的typedef

注意,我使用toupper和isspace,而不是直接查看char值。这意味着它将处理单词的开头,例如制表符,如果语言环境设置正确,它将把例如é转换为É。

string s=GetString;不是标准的C.string-这个数据类型在C中是什么?只要看看你的代码,想想它是做什么的。在你的脑海里一行一行地走过它。你很快就会意识到这是没有意义的。也许字符串是这样声明的:我认为GetString和string来自非常流行的,它似乎不时出现在这里。