0

したがって、ここでの課題は、文字列を受け取り、その文字列内の 2 番目の文字列に現れるすべての文字を削除するプログラムを設計することです。abしたがって、以下で選択した文字列では、最初の文字列が abc で 2 番目の文字列が cde であり、ではなくの出力を取得したいと考えていますabc

2 つの単純な for ループのみを必要とする、このスクイーズ関数を実行するための非常に巧妙な方法を既に見てきましたが、なぜ私の長く曲がりくねった方法が機能しないのか疑問に思っていました。

#include<stdio.h>

void squeeze(char s1[], char s2[]);
void copy(char to[], char from[]);

int k=0;

main()
{
    char array1[4]="abc";
    char array2[4]="cde";
    squeeze(array1, array2);
    printf("%s", array1);
}

void squeeze(char s1[], char s2[])
{
    int j,i,m;
    m=j=i=0;
    char s3[1000];
    while(s1[i]!='\0')   //What I'm doing here is taking a character in the string
    {                      //and comparing it with all characters in the second string.
        copy(s3,s1);       //If it exists in the second string I'm 'deleting' that letter
        while(s2[j]!='\0') //from the first string. Then I start all over again with the
        {                 // next letter in line. Or at least that's the plan.
            if (s1[i]==s2[j])
            {
                k=1;
            }
            ++j;
        }

        if (k==1)
        {
            m=i;
            while(s3[m+1]!='\0')
            {
                s1[m]=s3[m+1];  //I'm 'deleting' the letter by pushing each character
                ++m;            //that is to the right of the deleted character to the  
            }                   //left of the array.
        }

        if(k!=1)
        {
            ++i;
        }
    }
    s1[i]='\0';
 }

void copy(char to[], char from[])
{
    int i;
    i=0;

    while(from[i]!='\0')
    {
        to[i]= from[i];
        ++i;
    }
    to[i]=='\0';
}
4

2 に答える 2

2

「j」をゼロにリセットする必要がある間、アウターの内側に。

「k」が 1 になると、「i」はそれ以上インクリメントしません。2 度目に squeeze() を実行する場合、「k」を再度初期化する必要はありません。

"k" のようなグローバル変数 (またはモジュールのローカル変数) は決して使用しないでください。これにより、コードがスレッドセーフではなくなります。

于 2013-07-21T17:41:17.267 に答える
1

ハイタッチ-- 私もその章を読んでいます。4回くらい読んだと思いますが、とにかく実際にやって学ぶ必要があり、数時間で内容を全部忘れてしまいます。そのため、その章のすべての演習をほぼ終了しました。次は第 3 章です。

これが私の解決策です-スクイーズ関数はgetl​​ine関数と互換性がありません(標準出力に出力しないように/0'強制しています.printf

gcc 4.7.2でコンパイル

gcc -Wall -std=c99 -pedantic squeeze.c

#include <stdio.h>
#define LIM 100

void squeeze(char[], char[]);
int ggetline(char[], int);

int main()
{
    //the getline function is buggy; it would produce null strings.
    char line1[LIM] = "hello";
    char line2[LIM] = "hello2";
    squeeze(line1, line2);
    printf("%s %s\n", line1, line2);

return 0;

}


/* getline: reads line into s, returns length */
int ggetline(char s[], int lim)
{
    int c, i;

    for (i = 0; i < lim-1 && (c = getchar()) != EOF && c!='\n'; ++i)
        s[i] = c;
    if (c == '\n') {
        s[i] = c;
        ++i;
    }
    s[i] = '\0';
    return i;
}

void squeeze(char s1[], char s2[])
{
    int j, l, i, k;

    for (i = 0; s2[i] != '\0'; i++) {
        for (k = l = 0; s2[j] != '\0'; j++) {
            if (s1[j] != s2[i])
                s1[k++] = s1[j];
        }
        s2[k] = '\0';

    }
}

幸運を!

于 2013-07-21T20:43:31.607 に答える