Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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++ 每个单词的首字母大写_C++_String_Toupper - Fatal编程技术网

C++ 每个单词的首字母大写

C++ 每个单词的首字母大写,c++,string,toupper,C++,String,Toupper,输出与输入相同我在哪里出错? 请检查测试版本,它打印的是ASCII码“A”,而不是A。为什么会这样 循环中的第一个if条件是确保字符串仅以有效字符开头,而不是以空格开头 void caps(char* p) { char* begin=NULL; char* temp=p; while(*temp) { if((begin == NULL) && (*temp!= ' ')) begin=temp; if(

输出与输入相同我在哪里出错? 请检查测试版本,它打印的是ASCII码“A”,而不是A。为什么会这样

循环中的第一个if条件是确保字符串仅以有效字符开头,而不是以空格开头

void caps(char* p)
{
   char* begin=NULL;
   char* temp=p;

    while(*temp)
    {

      if((begin == NULL) && (*temp!= ' '))
        begin=temp;

      if(begin && ((*(temp+1) == ' ' ) || (*(temp+1)=='\0')))
      {
         toupper(*temp);
         begin=NULL;
      }

         temp++;
    }

 cout<<p;
}


int main()
{
  char str[]={"i like programming"};
  cout<< str <<endl;
  caps(str);
  return 0;
}
void caps(char*p)
{
char*begin=NULL;
char*temp=p;
while(*temp)
{
如果((begin==NULL)&&(*temp!='')
开始=温度;
if(begin&(*(temp+1)='')| |(*(temp+1)=='\0'))
{
toupper(*温度);
begin=NULL;
}
temp++;
}

cout好的,在上面链接的第一个代码
cout中:它被故意作为
int
返回,您可以简单地将
toupper
的返回转换回
char
,它将按照您的预期工作。好的,它在第二种情况下工作,但是我如何使它在第一种情况下工作?您正在做
toupper(*temp)
你应该做的是
*temp=toupper(*temp);
,因为
toupper
函数不使用引用,因此无法自动更改
*temp
。此外,此问题的标题说明您需要第一个大写字母d。您的代码看起来像是在更改单词的最后一个字符。您应该在执行
begin=temp
。代码中有一个错误,它确实更改了单词的最后一个字符。我正在尝试修复这个错误
#include <iostream>
#include <ctype.h>

using namespace std; 

int main()
{
   char a= 'a';
   cout<<toupper(a); //prints ASCII code of A(65) but why not 'A' ?
   return 0;
}
cout << (char)toupper(a);