method for expand a-z to abc…xyz form

前端 未结 4 773
孤街浪徒
孤街浪徒 2020-12-11 10:19

Hi:) what i\'m trying to do is write a simple program to expand from shortest entry

for example

a-z or 0-9 or a-b-c or a-z0-9

4条回答
  •  独厮守ぢ
    2020-12-11 10:49

    Ok I tested your program out and it seems to be working for nearly every case. It correctly expands a-z and other expansions with only two letters/numbers. It fails when there are more letters and numbers. The fix is easy, just make a new char to keep the last printed character, if the currently printed character matches the last one skip it. The a-z0-9 scenario didn't work because you forgot a s[i] >= '0' instead of s[i] > '0'. the code is:

    #include 
    #include 
    
    void expand(char s[])
    {
            int i,g,n,c,l;
        n=c=0;
        int len = strlen(s);
        for(i = 1;s[i] >= '0' && s[i]<= '9' || s[i] >= 'a' && s[i] <= 'z' || s[i]=='-';i++)
        {
            c = s[i-1];
            g = s[i];
            n = s[i+1];
            //printf("\nc = %c g = %c n = %c\n", c,g,n);
            if(s[0] == '-')
                printf("%c",s[0]);
            else if(g == '-')
            {
                if(c

    Isn't this problem from K&R? I think I saw it there. Anyway I hope I helped.

提交回复
热议问题