zoukankan      html  css  js  c++  java
  • 华为机试——首字母大写

    C_C++_BJH_01. Capitalize the First Letter

    • Description:

    Design a function that meets the following requirements:

    l Capitalize the first letter of each word in a character string.

    l If the first letter is capitalized or the first character is not a letter, leave the first character unchanged.

    • Function to be implemented:

    void vConvert2Upper(char *pInputStr, long lInputLen, char *pOutputStr);

    [Input] pInputStr: pointing to a character string

    lInputLen: length of the character string

    pOutputStr: pointing to a (lInputLen+1)–sized memory for output

    [Output] None

    [Note] Implement only the function algorithm, and do not perform any input or output.

    • Example

    Input: this is a Dog.

    Output: This Is A Dog.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54

    #include <iostream>
    #include <string.h>
    #include <ctype.h>
    using namespace std;
     
     
    void vConvert2Upper(char *pInputStr, long lInputLen, char *pOutputStr)
    {
        if ((pInputStr == NULL) || (lInputLen <= 0) || (pOutputStr == NULL))
        {
            return;
        }
     
        for (int i = 0; i < lInputLen; i++)
        {
            if ((i == 0) || (*pInputStr == ' '))
            {
                if (*pInputStr == ' ')
                {
     
                    *pOutputStr = *pInputStr;
                    pInputStr++;
                    pOutputStr++;
                }
     
                if (islower(*pInputStr))
                {
                    *pOutputStr = *pInputStr + 'A' - 'a';
                }
                else
                {
                    *pOutputStr = *pInputStr;
                }
            }
            else
            {
                *pOutputStr = *pInputStr;
            }
     
            pInputStr++;
            pOutputStr++;
        }
     
        *pOutputStr = '';
    }
     
    int main() {
     
        char *pInputStr = "this is a Dog.";
        char pOutputStr[20];
        vConvert2Upper(pInputStr, strlen(pInputStr), pOutputStr);
        cout << pOutputStr;
        return 0;
    }
  • 相关阅读:
    错误、异常与自定义异常
    关于使用第三方库、代码复用的一些思考
    [Scheme]一个Scheme的Metacircular evaluator
    [Scheme]Understanding the Yin-Yang Puzzle
    [Lua]50行代码的解释器,用来演示lambda calculus
    将jar包安装到本地仓库
    PowerDesigner安装教程(含下载+汉化+破解)
    Jmeter如何录制APP客户端脚本
    jdk1.8 stream 求和
    VMware的快照和克隆总结
  • 原文地址:https://www.cnblogs.com/helloweworld/p/3193485.html
Copyright © 2011-2022 走看看