Thread: array comparison

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    4

    array comparison

    Hi, I'm new at programming, and this is my first time doing arrays...i don't know what the problem with this code is. And where should i ask for the user input of the lines?

    Write a program to see if two lines from users are equivalent. They
    are considered equivalent if they use the same letters, case insensitive.
    The number of times a letter appears is NOT important. So AABCC is
    equivalent to cbba because they both use the letters a,b, and c. Beware,
    there may be spaces in the line.


    #include <stdio.h>

    int main() {

    int a[26], b[26], i,is_same;
    char ch;
    for(i=0; i<26; i++){
    a[i]=0;
    b[i]=0;
    }

    do{
    printf("Input a line of letters: ");
    scanf("%c", &ch);
    if(ch>='a'&&ch<='z')
    a[ch-'a']=1;

    if(ch>='A'&&ch<='Z')
    a[ch-'A']=1;
    }while(ch!='\n');

    do{
    printf("Input a line of letters: ");
    scanf("%c", &ch);
    if(ch>='a'&&ch<='z')
    b[ch-'a']=1;

    if(ch>='A'&&ch<='z')
    b[ch-'B']=1;
    }while(ch!='\n');

    is_same=1;
    for(i=0;i<=26;i++)
    if(a[i]!=b[i])
    is_same=0;
    if(is_same==0)
    printf("Not same");
    else
    printf("Same");

    return 0;

    }
    Code:
    http://cboard.cprogramming.com/forumdisplay.php?s=&forumid=4

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    you put the code tag around the wrong piece of information. The comparison seems really silly since all it cares about is if the same letters appear in both arrays.

    You seem to have the idea down. Just try running your code and find the problems.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: array comparison

    Originally posted by battoujutsu
    Hi, I'm new at programming, and this is my first time doing arrays...i don't know what the problem with this code is. And where should i ask for the user input of the lines?
    Neither do we, unless you tell us what you see when you run the program.

    Change your loops to:
    Code:
    printf("Input a line of letters: ");
    do
    {
        ch = getchar();
        if(ch>='a'&&ch<='z')  a[ch-'a']=1;
        if(ch>='A'&&ch<='Z')  a[ch-'A']=1;
    } while(ch!='\n');
    This way the trouble with scanf() won't bite you in the you-know-where.

    Code:
    int a[26], b[26], i,is_same;
    ....
    for(i=0;i<=26;i++)
    a & b are both 26 character arrays and the for loop goes to the 27th character -- a problem. Make the comparison simply i<26

    And put the code tags around the code, not a link.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    4
    Input a line of letters: ijk
    Input a line of letters: Input a line of letters: Input a line of letters: Input a line of letters: ijk
    Input a line of letters: Input a line of letters: Input a line of letters: Not same

    This is my output....

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    With that output you don't see the problem? Look at WaltP's changes and you should be dang close.

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    4
    Input a line of letters: ijk
    Input a line of letters: Input a line of letters: Input a line of letters: Input a line of letters: ijk
    Input a line of letters: Input a line of letters: Input a line of letters: Not same

    This is my output....

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by battoujutsu
    This is my output....
    1) Post your latest code.
    2) Use code tags correctly.
    3) Use preview to make sure it is correct.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    4
    Code:
    #include <stdio.h>
    
    int main() {
    
            int  a[26], b[26], i,is_same;
            char ch;
    
            for(i=0; i<26; i++){
                    a[i]=0;
                    b[i]=0;
            }
            printf("Input a line of letters: ");
            do{
                    ch =getchar();
                    scanf("%c", &ch);
                    if(ch>='a'&&ch<='z')
                            a[ch-'a']=1;
                    if(ch>='A'&&ch<='Z')
                            a[ch-'A']=1;
                    }while(ch!='\n');
    
            printf("Input a line of letters: ");
            do{
                    ch=getchar();
                    scanf("%c", &ch);
                    if(ch>='a'&&ch<='z')
                            b[ch-'a']=1;
    
                    if(ch>='A'&&ch<='z')
                            b[ch-'B']=1;
                    }while(ch!='\n');
    
                            is_same=1;
    
                    for(i=0;i<26;i++){
                            if(a[i]!=b[i])
                                    is_same=0;
                            if(is_same==0)
                            printf("Not same\n");
                    else
                            printf("Same");
    }
    
                    return 0;
    
    }
    //Output:
    Input a line of letters: AAbc

    Input a line of letters: accb

    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same
    Not same

  9. #9
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    ch =getchar(); /* read character from input */
    scanf("%c", &ch); /* read character from input */
    Why the scanf? The getchar function already reads a character from input. Take a look again at WaltP's code

  10. #10
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    And another thing:
    Code:
    if(ch>='A'&&ch<='z')
       b[ch-'B']=1;

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
                    for(i=0;i<26 && is_same==1;i++){
                            if(a[i]!=b[i])
                                    is_same=0;
                            }
    
                    if(is_same==0)
                            printf("Not same\n");
                    else
                            printf("Same");

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    To add: get rid of the scanfs and change your case and the program should work.

  13. #13
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Also, fix your formatting. You shold be indenting blocks, not everything:
    Code:
    
    int main()
    {
        indent code
        while()
        {
            indent code
            if ()
            {
                indent code
            }
            else
            {
                indent code
            }
            indent code
        }  // end of while
        do
        {
            indent code
        } while ();
        return 0;
    }
    
    There is discussion about where to put the open brace '{' but the indenting should be like above.
    1) Once a { is used, indent 3-4 spaces
    2) as a } is used, restore previous indent
    This will help you find misplaced statements (like printf()) that should be outside a loop but are actually inside.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  2. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM