问题及代码:
/*
*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';
}
运行结果:
去除句子中第一个单词前的空格(指针作形参)。
学习心得:
还是自定义函数不好写啊啊啊!
我真是太不熟练了!
↖(^ω^)↗加油加油~