您的位置:首页 > 其它

第16周项目2-去除句子中第一个单词前的空格(指针作形参)

2014-12-15 12:51 316 查看
问题及代码:

/* 
*Copyright (c)2014,烟台大学计算机与控制工程学院 
*All rights reserved. 
*文件名称:num.cpp 
*作    者:单昕昕 
*完成日期:2014年12月15日 
*版 本 号:v1.0 
* 
*问题描述:去除句子中第一个单词前的空格。 
*程序输入:无。
*程序输出:去除句子中第一个单词前的空格后的句子。
*/

#include <iostream>
using namespace std;
char *prim(char *str);
int main()
{
    char s[100]="     What hurts more, the pain of hard work, or the pain of regret? ";
    cout<<"/"<<s<<endl;
    cout<<"去除第一个空格后:"<<endl;
    cout<<"/"<<prim(s);
    return 0;
}
char *prim(char *str)
{
    char *p=str,*q=str;
    if(*q==' ')
        q++;
    while(*q!='\0')
        *p++=*q++;
    *p='\0';
    return (str);
}

#include <iostream>
using namespace std;
void ptrim(char *str);
int main()
{
    char s[100]="     What hurts more, the pain of hard work, or the pain of regret? /";
    cout<<"/"<<s<<endl;
    cout<<"去除第一个空格后:"<<endl;
    ptrim(s);
    cout<<"/"<<s<<endl;
    return 0;
}
void ptrim(char *str)
{
    char *p=str,*q=str;
    if(*q==' ')
        q++;
    while(*q!='\0')
        *p++=*q++;
    *p='\0';
}


运行结果:



知识点总结:

去除句子中第一个单词前的空格(指针作形参)。

学习心得:

还是自定义函数不好写啊啊啊!

我真是太不熟练了!

↖(^ω^)↗加油加油~


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐