经营管理类书籍推荐:C语言的朋友帮我编一下这题啊~~急!!

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/29 23:05:32
编写一个函数fun(),其功能是删除一个字符串指定的字符。要求原始字符串在主函数中输入,处理后的字符串在主函数中输出。如有一字符串为“I am a teacher,you are a student.”,欲将该串中的所有字符“e”删除,则删除后的字符串为“I am a tachr,youbar a studnt.”

// zd_18.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

char str[100];

void fun()
{
int i,j;
for(i=0;i<100;i++)
{
if(str[i]=='e')
{
for(j=i;j<100;j++)
str[j]=str[j+1];
}
}
}

int main(int argc, char* argv[])
{
// scanf("%s",&str);
char ch;
for(int i = 0;(i < 100) && ((ch = getchar()) != EOF)
&& (ch != '\n'); i++ )
str[i] = (char)ch;
fun();
printf("%s\n",str);
return 0;
}
输出:
i am a teacher.you are a student.
i am a tachr.you ar a studnt.
Press any key to continue

/* delet char 'e' in a string */
#include<stdio.h>

char* fun(char* str2);

main()
{
char str[100];
gets(str);
fun(str);
printf("%s", str);
printf("\n");
}

char* fun(char *str2)
{
int i,j=0;

for(i=0; i<100; i++,j++)
{
if(str2[j] != 'e')
str2[i] = str2[j];
else
i--;
}

return str2;
}