问题及代码:
/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:test.cpp
*作 者:陈文青
*完成日期:2014年12月15日
*版 本 号:v1.0
*
*问题描述:去除字符串str中的特定字符c(如空格),结果仍保存到原字符串中
*程序输入:
*程序输出:
*/
#include <iostream>
#include <string>
using namespace std;
char* adelchar(char str[], const char c);
char* pdelchar(char *str, const char c);
int main(void)
{
char s[50]="Hello world.";
char a[50]="You are the best friend for me.";
adelchar(s,' ');
cout<<"去除空格后,字符串为:"<<s<<endl;
pdelchar(a, ' ');
cout<<"去除空格后,字符串为:"<<a<<endl;
return 0;
}
char* adelchar(char str[], const char c)
{
int i=0,j;
for(j=0; str[j]!='\0'; j++)
{
if(str[j]!=' ')
str[i++]=str[j];
}
str[i]='\0';
return str;
}
char* pdelchar(char *str, const char c)
{
int i=0,j;
for(j=0; *(str+j)!='\0'; j++)
{
if(*(str+j)!=' ')
*(str+i++)=*(str+j);
}
*(str+i)='\0';
return str;
}
运行结果
:
知识点总结:
指针变量与数组对比
去除字符串中特定字符