繁体   English   中英

不使用string.h库从字符串中删除字符

[英]remove characters from string without using string.h library

我正在做一个作业,它要求不使用string.h库从字符串中删除一些字符,这是我的代码:

 int deleteFunc(char chara[100], char a)
{
    int i ;
    for ( i = 0 ; i < 100; i++)
      if (a == chara[i])
       return TRUE;
   else return FALSE;
}

我的deleteFunc虽然有一些问题,但我不知道该如何解决,任何人都可以提供帮助。 谢谢!

deleteFunc更改为

int deleteFunc(char chara[100], char a){
    int i ;
    for ( i = 0 ; i < 100 && chara[i]; i++)//'\0' after the garbage data
        if (a == chara[i])
            return TRUE;
    return FALSE;//If that does not match all of the candidate
}

和主要

*pointer = '\0';
printf("String after removing is \"%s\"\n", start);//Display prior to release, or written back

请看下面的代码可能会有所帮助。

int main()
{
    int i=0,j=0,k=0,q;
    char a[20],b[20];
    char *p = malloc(20);
    scanf("%s %s",a,b);
     while(a[i] != '\0')
     {
         j=0;
         q =0;
         while(b[j] != '\0')
         {
            if(a[i] == b[j])
            {
                q  =1;
                break;
            }
            j++;
         }
         if(!q)
         p[k++] = a[i];
         i++;
     }
     p[k] = '\0';
     printf("%s\n",p);
     free(p);
     return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM