博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
华为机试——字符倒叙输出
阅读量:4708 次
发布时间:2019-06-10

本文共 1520 字,大约阅读时间需要 5 分钟。

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 != '\0')
    {
        head = tail;
        while (isalpha(*tail) && (*tail != '\0')) //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 != '\0')) //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;
}

转载于:https://www.cnblogs.com/helloweworld/p/3200860.html

你可能感兴趣的文章
jquery 取id模糊查询
查看>>
解决在vue中,自用mask模态框出来后,下层的元素依旧可以滑动的问题
查看>>
SSH加固
查看>>
python 二维字典
查看>>
实验吧之【天下武功唯快不破】
查看>>
2019-3-25多线程的同步与互斥(互斥锁、条件变量、读写锁、自旋锁、信号量)...
查看>>
win7-64 mysql的安装
查看>>
dcm4chee 修改默认(0002,0013) ImplementationVersionName
查看>>
maven3在eclipse3.4.2中创建java web项目
查看>>
Building Tablet PC Applications ROB JARRETT
查看>>
Adobe® Reader®.插件开发
查看>>
【POJ 3461】Oulipo
查看>>
Alpha 冲刺 (5/10)
查看>>
使用Siege进行WEB压力测试
查看>>
斑马为什么有条纹?
查看>>
android多层树形结构列表学习笔记
查看>>
Android_去掉EditText控件周围橙色高亮区域
查看>>
《构建之法》第一、二、十六章阅读笔记
查看>>
Git Stash用法
查看>>
Jquery radio选中
查看>>