簡體   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