zoukankan      html  css  js  c++  java
  • 将一个句子中单词的首字母转换为大写

    如:

         hello my name is zeroinger , nice to meet you!

    转换后:

         Hello My Name Is Zeroinger , Nice To Meet You!

    代码:

        

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <string>
    #include <cctype>
    using namespace std;
    string slove(string& str)
    {
        //string 指针指向字符串首地址
        string::iterator it =str.begin();
        //空格标志位
        bool flag_space=true;
        //循环遍历句子
        while(it!=str.end())
        {   //是否是一个单词的首字母
            if(isalpha(*it) && flag_space)
            {
    
                *it=toupper(*it);
              //  it++;
                flag_space = false;
            }
            //如果是空格,标志位置1
            if(isspace(*it))
            {
                flag_space=true;
            }
            it++;
        }
        return str;
    }
    int main()
    {
        //字符串读入方式也该注意
         string str1,str2;
        // str1 = "hello my name is zeroinger , nice to meet you!";
         getline(cin,str1);
         str2=slove(str1);
         cout<<str2<<endl;
         return 0;
    }
    

      

        

  • 相关阅读:
    48-最长不含重复字符的子字符串
    51-数组中的逆序对
    字符串的排列
    二叉树转链表
    求根
    构造二叉树
    二叉树中序遍历
    反转链表系列
    斐波那契系列
    f.lux
  • 原文地址:https://www.cnblogs.com/Zeroinger/p/5572375.html
Copyright © 2011-2022 走看看