问题及代码:
/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights reserved.
*文件名称:莉莉.cpp
*作者:李莉
*完成日期:2014年12月14日
*版本号:v1.0
*
*问题描述:利用指针,完成字符串的处理 删除字符串中的空格
*程序输入:无输入
*程序输出:处理后的字符串
*/
#include <iostream>
#include <cstdio>
using namespace std;
void ptrim(char *str);
int main()
{
char s[81];
cout<<"请输入一个句子:"<<endl;
gets(s);
ptrim(s);
cout<<"删掉开始的空格的句子是:"<<s<<endl;
return 0;
}
void ptrim(char*str)
{
char *p=str,*q=str;
while (*q==' ')
{
q++;
}
while(*q!='\0')
{
*p++=*q++;
}
*p='\0';
}
运行结果:
心得体会:在刚开始的时候,p++和*p++弄混了