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;
    }
  • 相关阅读:
    web api authentication
    平常项目中缓存使用经验和遇到过的问题(3)
    平常项目中缓存使用经验和遇到过的问题(2)
    平常项目中缓存使用经验和遇到过的问题(1)
    DllImport attribute的总结
    人生路上对我影响最大的三位老师
    自我介绍
    7-1 币值转换 (20 分)
    7-1 打印沙漏 (20 分)
    form表单自动提交
  • 原文地址:https://www.cnblogs.com/helloweworld/p/3193485.html
Copyright © 2011-2022 走看看