这个容易,就是把指定位置的字符去掉,也就是说后面的字符覆盖。
比如删除指定的字符在字符串中第一个出现的位置。
void
strdel(
char*
str,
char
ch
char
*p
str;
while(
*p
if(
*p==ch
break;
if(
*p
while(
*p
*p==*(p+1);
p++;
希望能解决您的问题。
//#include "stdafx.h"//vc++6.0加上这一行.。
#include "stdio.h"。
void del_char (char *p,char ch){。
int i,j;
for(j=i=0;p[i];i++)。
if(p[i]!=ch)。
p[j++]=p[i];。
p[j]=p[i];
int main(void){。
char a,str[100];。
scanf ("%c",&a);。
scanf ("%s",str);。
del_char (str,a);。
puts(str);
return 0;
#include <stdio.h>。
#include <string.h>。
void delchar(char *s, char c_h)。
int len = (int)strlen(s);。
int count=0,i;。
while(*(s+count)!='\0')。
{
if(*(s+count) == c_h)。
{
for(i=count; i<len; i++)。
{
*(s+i)=*(s+i+1);。
}
len--;
}else
{
count++;
}
}
int main()
char s[256],c;
printf("input s c:\n");。
scanf("%s %c",s,&c);。
delchar(s,c);
printf("str:%s",s);。
#include<stdio.h>。
char*del_char(char*str,charch)。
unsignedchari=0,j=0;。
while(str[i]!='\0')。
if(str[i]!=ch)//只有在不是空格的情况下目标才会移动赋值。
str[j++]=str[i];。
i++;//源一直移动
str[j]='\0';
returnstr;
intmain(intargc,constchar*argv[])。
charch;
inti=0;
chara[1024]={'\0'};。
while((ch=getchar())!='\n')。
a[i++]=ch;
char*s=del_char(a,'');。
puts(s);
return0;
扩展资料
去除字符串的()字符
#include<stdio.h>。
#include<stdlib.h>。
#include<string.h>。
#include<strings.h>。
/**
*去除字符串的()字符
*(ASCII32(0x20))空格符(ASCII9(0x09))制表符(ASCII13(0x0D))回车符(ASCII10(0x0A))换行符。
一、问题描述:从键盘输入一个字符串给str和一个字符给c,删除str中的所有字符c并输出删除后的字符串str。
1、输入:第一行是一个字符串; 第二行是一个字符。
2、输出:删除指定字符后的字符串。
二、设计思路:
1、 同插入问题,定义两个字符数组a,b。以及标志删除位置的int型pos。
2、用gets函数输入数组a的值,并利用for循环将数组a copy到 数组b。
3、利用for循环,令pos位的数组b元素赋值到a。
三、实现代码如下:
四、编译并执行,运行结果如下:
扩展资料
gets()函数用来从标准输入设备(键盘)读取字符串直到换行符结束,但换行符会被丢弃,然后在末尾添加'\0'字符。其调用格式为:gets(s),其中s为字符串变量(字符串数组名或字符串指针)。
gets()函数读取到\n(我们输入的回车)于是停止读取,但是它不会把\n包含到字符串里面去。然而,和它配合使用的puts函数,却在输出字符串的时候自动换行。
gets(s) 函数中的变量s为一字符串指针。如果为单个字符指针,编译连接不会有错误,但运行后内存溢出错误。宽字符版本,当使用unicode宽字符文本时,使用这个函数 _getws();在C11标准中被删除,可用C标准库中的fgets代替.。
参考资料:百度百科 gets