zoukankan      html  css  js  c++  java
  • 华为机试——字符倒叙输出

    C_C++_WY_01. 字符倒叙输出

    • 题目描述:

    编写一个函数,将字符串中的每个单词的倒序输出,字符串中以空格分割各个单词,如果碰到数字则跳过。

    • 要求实现函数:

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

    【输入】

    char *pInputStr:指向一个字符串的指针

    long lInputLen:该字符串的长度

    char *pOutputStr:指向一块输出的内存,和输入的字符串是大小是(lInputLen+1)

    【返回】 无

    【注意】 只需要完成该函数功能算法,中间不需要有任何IO的输入输出

    • 示例

    输入:He is a man no12 3456.

    返回:eH si a nam on12 3456.

    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
    55
    56
    57
    58
    59
    60
    61

    #include <iostream>
    #include <string.h>
    #include <ctype.h>
    using namespace std;
     
    void convertWord(char *head, char *tail)
    {
        char tmp;
     
        while (head < tail)
        {
            tmp = *head;
            *head = *tail;
            *tail = tmp;
     
            head++;
            tail--;
        }
    }
     
    void vConvertMsg(char *pInputStr, long lInputLen, char *pOutputStr)
    {
        if ((pInputStr == NULL) || (lInputLen <= 0))
        {
            return;
        }
     
        char *head = pInputStr;
        char *tail = pInputStr;
     
        while (*tail != '')
        {
            head = tail;
            while (isalpha(*tail) && (*tail != '')) //find the end of a word.
            {
                tail++;
            }
     
            tail--; //point to the end of a word.
            convertWord(head, tail);
     
            tail++; //point to the first char that it's not a alpha.
            while ((!isalpha(*tail)) && (*tail != '')) //find the head of a word.
            {
                tail++;
            }
        }
     
        strcpy(pOutputStr, pInputStr);
    }
     
    int main() {
        char pInputStr[] = "He is a man no12 3456";
        char pOutputStr[strlen(pInputStr)+1];
     
        vConvertMsg(pInputStr, strlen(pInputStr), pOutputStr);
     
        cout << pOutputStr << endl;
     
        return 0;
    }
  • 相关阅读:
    业务领先模型(Business Leadership Model; BLM)
    快速了解云安全态势管理(CSPM)
    《贸易打造的世界:1400年至今的社会、文化与世界经济》笔记
    《消费社会》笔记
    《向上生长 看懂趋势,掌控未来》
    DSCMM 数据安全能力成熟度模型
    薪火
    《贸易的冲突:美国贸易政策200年》笔记
    近年火热的“信创”到底是什么
    什么是“以数据为中心的安全”?(一) —— 大家眼中的DCS
  • 原文地址:https://www.cnblogs.com/helloweworld/p/3200860.html
Copyright © 2011-2022 走看看