C字符串的练习

来源:百度知道 编辑:UC知道 时间:2024/05/26 06:10:41
题目:编写一个名为removestring的函数,该函数用于从一个字符串中删除一定量的字符。该函数接受三个参数:第一个参数代表源字符串,第二个参数代表需要删除字符的起始位置,第三个参数代表需要删除的字符个数。因此如果字符数组text包含的字符串"the wrong son",下面的调用语句:
removestring(text,4,6);
则会删除该字符数组中的字符串wrong以及后面的空格。空字符数组中的遗留内容则是字符串"the son"。

想参照一下大家的写法,谢谢~~
不能用指针,只能使用#include<stdio.h> 、#include <stdbool.h>、#include <conio.h>

#include <stdio.h>
#include <stdbool.h>
#include <conio.h>

void removestring(char text[], int count, int word)
{

char source[81];
int i, n = 0;

for ( i = 0; i < count; ++ i)
source[i] = text[i];

for ( ; text[ i + word ] != '\0'; ++ i )
source[ i + n ] = text[ word + i];

source[ i + n ] = '\0';
text = source;

printf("%s\n",source);

}

int main(void)
{
char text[] = "the wrong son";

removestring (text, 10, 3);

getch();
return 0;
}

#include<stdio.h>
#include <string.h>
char *removestring(char *s, size_t begin, size_t length)
{
memmove(s + begin, s + begin + length, strlen(s)-begin-length+1);
return s;
}

int main()
{
char text[] = "the wrong son";