将每个单词中的首字母大写

时间:2015-04-22 11:35:13

标签: c++ string toupper

  

输出与我输错的输入相同?   请检查测试版本,它打印的是“A”的ASCII码而不是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;
}
  

测试

如果我使用printf(“%c”,toupper(a)),它会正确打印'A'。

#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;
}

0 个答案:

没有答案
相关问题