鱼C论坛

 找回密码
 立即注册
查看: 1309|回复: 6

[已解决]字符串中删除一部分字母

[复制链接]
发表于 2021-1-29 13:56:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这个只能实现删除指定字母
如何实现删除指定字符串呢

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char array[50];
    gets(array);                   //输入字符串
    char change;
    scanf("%c",&change);    //输入指定字母

    int i,N;
    N=strlen(array);
    for(i=0;i<N;i++)
    {
        if(array[i]!=change)

        printf("%c",array[i]);     //在字符串中删除指定字母
    }


    return 0;
}


最佳答案
2021-1-29 14:38:10
本帖最后由 心驰神往 于 2021-1-29 14:39 编辑
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. int main()
  5. {
  6.     char array[50];
  7.     gets(array);                   //输入字符串
  8.     /*char change;
  9.     scanf("%c",&change);    //输入指定字母*/
  10.     char change[50];
  11.     gets(change);

  12.     int i,N;
  13.     N=strlen(array);
  14.     for(i=0;i<N;i++)
  15.     {
  16.         if(array[i]!=change[i])

  17.         printf("%c",array[i]);     //在字符串中删除指定字符串
  18.     }


  19.     return 0;
  20. }
复制代码

运行结果:
  1. qwerewwqr
  2. qwe
  3. rewwqr
  4. --------------------------------
  5. Process exited after 4.953 seconds with return value 0
  6. 请按任意键继续. . .







复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-29 14:34:00 | 显示全部楼层
  1.        array[0] = '\0'        // 删除字符串 array
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-29 14:38:10 | 显示全部楼层    本楼为最佳答案   
本帖最后由 心驰神往 于 2021-1-29 14:39 编辑
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. int main()
  5. {
  6.     char array[50];
  7.     gets(array);                   //输入字符串
  8.     /*char change;
  9.     scanf("%c",&change);    //输入指定字母*/
  10.     char change[50];
  11.     gets(change);

  12.     int i,N;
  13.     N=strlen(array);
  14.     for(i=0;i<N;i++)
  15.     {
  16.         if(array[i]!=change[i])

  17.         printf("%c",array[i]);     //在字符串中删除指定字符串
  18.     }


  19.     return 0;
  20. }
复制代码

运行结果:
  1. qwerewwqr
  2. qwe
  3. rewwqr
  4. --------------------------------
  5. Process exited after 4.953 seconds with return value 0
  6. 请按任意键继续. . .







复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-29 15:50:08 | 显示全部楼层

QAQ      这个测试案例就不对了  这还能改吗

qwwewewwqw
qw
wewewwqw
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-29 15:57:19 | 显示全部楼层
丸子酱ovo 发表于 2021-1-29 15:50
QAQ      这个测试案例就不对了  这还能改吗

qwwewewwqw

你想把后面那个qw也去了?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-29 16:07:56 | 显示全部楼层
丸子酱ovo 发表于 2021-1-29 15:50
QAQ      这个测试案例就不对了  这还能改吗

qwwewewwqw

上面那个确实有问题
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "string.h"
  4. void delet(char *s1,char *s2);
  5. int main()
  6. {
  7.     char s1[80],s2[80];
  8. printf("请输入一个字符串\n");
  9. scanf("%s",s1);
  10. printf("请输入一个要删除的子串\n");
  11. scanf("%s",s2);
  12. delet(s1,s2);
  13. }
  14. void delet(char *s1,char *s2){
  15.     char *p1,*p2,result[80]={'\0'};
  16.     p2=s1;
  17.     while((p1=strstr(s1,s2))!=NULL)
  18.     {
  19.         strncat(result,s1,p1-p2);
  20.         strcpy(s1,p1+strlen(s2));
  21.         p2=s1;  
  22.     }
  23.     strcat(result,s1);
  24.     printf("%s\n",result);
  25. }
复制代码

这个可以,出自https://blog.csdn.net/z2541498852/article/details/78841167
运行结果:
  1. 请输入一个字符串
  2. qwerdf1234qw
  3. 请输入一个要删除的子串
  4. qw
  5. erdf1234

  6. --------------------------------
  7. Process exited after 15.69 seconds with return value 0
  8. 请按任意键继续. . .

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-29 16:26:51 | 显示全部楼层
#include "stdio.h"
#include "string.h"
int main()
{
    int i=0,x=0;
    char *p=NULL,*t=NULL,s[100],d[50];
   
    printf("输入字符串:");
    gets(s);
    printf("输入要删除的字符串:");
    gets(d);
   
    if( (p=strstr(s,d)) == NULL)
    {
        printf("\n\n字符串1: %s\n中没有找到\n字符串2: %s\n",s,d);
        return 0;
    }
   
    while((p=strstr(s,d)) != NULL)
    {
        t=p+strlen(d);
        *p='\0';
        strcat(s,t);
        x++;
    }
   
    printf("共删除%d处\n%s\n",x,s);
   
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-2 03:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表