K&安培; R运动挤压作用 [英] K&R Exercise Squeeze function

查看:98
本文介绍了K&安培; R运动挤压作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,这里的运动是设计一个程序,它接受一个字符串,并删除所有字符在出现的第二个字符串中的字符串。因此,与我选择以下其中一个字符串是ABC和第二串的串是CDE我想获得 AB ,而不是的输出ABC

我已经看到了一个非常整洁的方式做到这一点挤压功能,只需要两个简单的循环,但我想知道为什么我的长篇大论方式是行不通的。

 #包括LT&;&stdio.h中GT;无效挤压(字符S1 [],CHAR S2 []);
无效副本(字符为[],在[]字符);INT K = 0;主要()
{
    炭ARRAY1 [4] =ABC;
    炭数组2 [4] =CDE;
    挤压(数组1,数组2);
    的printf(%S,数组1);
}无效挤压(字符S1 [],CHAR S2 [])
{
    诠释J,I,M;
    M = J = = 0;
    炭S3 [1000];
    而(S1 [I]!='\\ 0')//我在做什么在这里走的是一条字符字符串
    {//以及与第二字符串中的所有字符进行比较的。
        副本(S3,S1); //如果第二个字符串中存在我'删除'的信
        而(S2 [J]!='\\ 0'),从第一个字符串//。然后,我与从头再来
        本着{//下一个字母。或者说,至少是这样的计划。
            如果(S1 [I] == S2 [J]。)
            {
                K = 1;
            }
            ++焦耳;
        }        如果(K == 1)
        {
            M = 1;
            而(S3 [M + 1]!='\\ 0')
            {
                S1 [米] = S3 [M + 1]; //我推的每个字符'删除'信
                ++米; //这是被删除的字符的权
            } //左边的数组。
        }        如果(K!= 1)
        {
            ++我;
        }
    }
    S1 [I] ='\\ 0';
 }无效副本(字符为[],在[]字符)
{
    INT I;
    I = 0;    而(从[I]!='\\ 0')
    {
        在[I] =从[I]
        ++我;
    }
    在[I] =='\\ 0';
}


解决方案

你的外在内,而应该J重置为零。

如果Kbekomes 1,你不增加的我了。如果您运行挤()第二次,你没有初始化K了。

从未使用全局变量(或模块局部变量)和K。这使得你的code线程安全的。

So the exercise here is to design a program that takes a string and removes all characters in that string that appear in the second string. So with the strings I've chosen below where the first string is abc and the second string is cde I want to get an output of ab rather than abc.

I've already seen a very neat way to do this squeeze function that only requires two simple for loops but I was wondering why my long winded way doesn't work.

#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';
}

解决方案

inside your outer while you should reset "j" to zero.

if "k" bekomes 1, you do not increment "i" any more. if you run squeeze() a second time, you do not initialize "k" again.

never use global variables (or module local variables) like "k". This makes your code thread-unsafe.

这篇关于K&安培; R运动挤压作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆