、在主程序中输入一个字符串和一个整数n,将字符串中第n个字符删除,要求删除字符的操作通过函数delete(char *p, int n)实现。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 10
void Delete(char* p, int n)
{
for (p = p + (n-1);; ++p)
{
if (*(p + 1) == '\0')
break;
*p = *(p + 1);
}
*(p) = '\0';
}
int main()
{
char a[N];
int n;
gets(a);
scanf("%d", &n);
char* p = a;
Delete(p, n);
int i;
int length = strlen(a);
for (i=0;i<length;i++)
{
printf("%c", a[i]);
}
return 0;
}
该程序使用C语言编写,功能是删除输入字符串中的第n个字符。通过Delete函数实现,该函数遍历字符串,将目标位置后的字符前移覆盖目标字符,并更新字符串结束符。在主函数中,用户输入字符串和要删除的位置n,然后输出删除后的字符串。
1040

被折叠的 条评论
为什么被折叠?



