编写函数,能处理字符串中除字母、数字外的其他ASCII字符,对连在一起的相同字符,使其缩减至仅保留一个?

(c语言)
(参考函数原型:void del(char* str))
输入输出格式要求:
只编写函数,请勿输出其他字符
例如:
str为:aa*++1123.,
调用del函数之后str为:aa*+1123.,

实在不会了QAQ谢谢大佬拯救

按照题目要求编写的缩减相同字符的C语言程序如下

#include<stdio.h>

#include<string.h>

void del(char* str){

 int len,i,j;

 len=strlen(str);

 for(i=1;i<len;i++){

  if('a'<=*(str+i) && *(str+i)<='z' || 'A'<=*(str+i) && *(str+i)<='Z' || '0'<=*(str+i) && *(str+i)<='9'){

  }else{

  if(*(str+i-1)==*(str+i)){

    for(j=i;j<len;j++){

     *(str+j)=*(str+j+1);

    }

    i--;

    len=len-1;

   }

  }

 }

}

int main(){

 char s[100];

 fgets(s,100,stdin);

 del(s);

 printf("%s",s);

 return 0;

}

追问

QAQ大佬这个len--是为啥啊

追答

因为比如有字符串"abc++de",执行for_j循环后"de"向前移覆盖后面的加号,变成了"abc+de"字符串总长度短了一,所以下次for循环前要总长度减一.

追问

啊好的,
谢谢大佬
爱了

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-10-30

void del(char* str)

{

int i = 0,j;

while(str[i] != '\0')

{

if(str[i] >= '0' && str[i] <= '9' && str[i] >= 'A' && str[i] <= 'Z' && str[i] >= 'a' && str[i] <= 'z')

;

else if(str[i] == str[i-1])

{

j = i;

while(str[j] != '\0')

{

str[j] = str[j+1];

j++;

}

}

i++;

}

}

第2个回答  2020-10-30
函数如下:
void del(char*str)
{
char*p1,*p2,c;
p1=p2=str;
p2++;
c=*p1;
do
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z'||c>='0'&&c<='9')
{
*(++p1)=*p2++;
c=p2++;
}
else
{
if(c!=*p2)
{
*(++p1)=*p2++;
c=*p1;
}
else
p2++;
}
}while(c);
}
有什么问题请留言。
相似回答
大家正在搜