Thread: I need help in using strings and char

  1. #1
    Registered User
    Join Date
    Apr 2015
    Location
    Reine, Hazafon, Israel, Israel
    Posts
    11

    I need help in using strings and char

    So I have this homework about Elections.
    the user inputs 3 letters , each one represents a group of parties .
    for example the input is : A D H
    first group of parties : A B C
    second : D E F G
    third : H-Z
    the next input will be the votes , doesnt matter a capital letter or small . if the inpute of votes for examlpe is : aBcEgQ
    the output should be that :
    first group got : 3
    second: 2
    third:1
    now to the problem im facing , im using this code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        char a[100];
        char X,Y,Z;
        int score1=0;
        int score2=0;
        int score3=0;
    
    
        printf("Welcome to the 2015 elections!\n");
    
    
        printf("Please enter the camps leaders:\n");
        scanf(" %c %c %c", &X , &Y, &Z);
        printf("Please enter the votes:\n");
        scanf(" %s",a);
        int n=0 ;
    
    
        for(int i = n ; i<=100 ; i++ )
        {
            if( X>=a[i] )
            {
                score1++;
            }
            if(X<a[i] && a[i]<=Y)
            {
                score2++;
            }
            if(Y<a[i] && a[i]<=Z)
            {
                score3++;
            }
    
    
        }
    
    
        printf("score1 is : %d\n",score1);
        printf("score1 is : %d\n",score2);
        printf("score1 is : %d\n",score3);
    
    
    
    
    
    
    
    
    
    
        return 0;
    }
    the problem is that if I entered the input as explained in the example above the output is :
    score1 is :81
    score2 is :1
    score3 is :3
    what is the problem !? I think im missing something or things actually :\
    thanks guys

  2. #2
    Registered User
    Join Date
    Mar 2015
    Location
    BE
    Posts
    66
    What do you mean by "doesn't matter a capital letter or small"?
    All capital letters are "smaller" than 'a' for example.
    Try to fix that by your own code or make use of some functions in <ctype.h>.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Location
    Reine, Hazafon, Israel, Israel
    Posts
    11
    Quote Originally Posted by Carnotter View Post
    What do you mean by "doesn't matter a capital letter or small"?
    All capital letters are "smaller" than 'a' for example.
    Try to fix that by your own code or make use of some functions in <ctype.h>.
    okay lets say the input is just small letters ,, why it doesnt count them right ?!? im not finding the problem in my code ! it is supposed to count the letter (b) for example each time one of them appears in the input and add it to score1 .. instead it gives me a number 80+

  4. #4
    Registered User
    Join Date
    Mar 2015
    Location
    BE
    Posts
    66
    The first question TheShadow, how would you solve the problem in your mind before thinking how to program it?
    Last edited by Carnotter; 04-03-2015 at 04:59 PM. Reason: was string nor char

  5. #5
    Registered User
    Join Date
    Apr 2015
    Location
    Reine, Hazafon, Israel, Israel
    Posts
    11
    it's okay i solved it i added an '!' after we input the votes
    so this part of the code becomes
    Code:
        for(int i = 0 ; a[i]!='!' ; i++ )
    looks like the problem was when it checks all the letters till 100 IDK something like that
    Last edited by TheShadow; 04-03-2015 at 05:01 PM.

  6. #6
    Registered User
    Join Date
    Mar 2015
    Location
    BE
    Posts
    66
    If the input is: A C F
    First group? A and B
    Second? C
    Third? From D to Z

    Is that what you mean?
    if not, you should first check if a range of input is ok.

  7. #7
    Registered User
    Join Date
    Apr 2015
    Location
    Reine, Hazafon, Israel, Israel
    Posts
    11
    yes this is what i meant

  8. #8
    Registered User
    Join Date
    Apr 2015
    Location
    Reine, Hazafon, Israel, Israel
    Posts
    11
    now my second problem is how to make it count both capital and small letter as one :\
    well using Ascii ,, 'A' is 'a'-32 am I in the right path of thinking ?! or there is an easier way ?!

  9. #9
    Registered User
    Join Date
    Mar 2015
    Location
    BE
    Posts
    66
    Quote Originally Posted by TheShadow View Post
    it's okay i solved it i added an '!' after we input the votes
    so this part of the code becomes
    Code:
        for(int i = 0 ; a[i]!='!' ; i++ )
    looks like the problem was when it checks all the letters till 100 IDK something like that
    Are you sure it's solved? I tested it with your example (A D H and aBcEgQ). I got:
    Score1: 1748
    Score2: 9
    Score3: 12
    Yes, the list a isn't really empty when you make the declaration within main().

  10. #10
    Registered User
    Join Date
    Apr 2015
    Location
    Reine, Hazafon, Israel, Israel
    Posts
    11
    Quote Originally Posted by Carnotter View Post
    Are you sure it's solved? I tested it with your example (A D H and aBcEgQ). I got:
    Score1: 1748
    Score2: 9
    Score3: 12
    Yes, the list a isn't really empty when you make the declaration within main().
    try it without capital letters it works ,, i'm trying to fix this thing ,, to make it work both of capital and small letters
    also make sure you type '!' after the vote so the loop can stop there

  11. #11
    Registered User
    Join Date
    Mar 2015
    Location
    BE
    Posts
    66
    Quote Originally Posted by TheShadow View Post
    now my second problem is how to make it count both capital and small letter as one :\
    well using Ascii ,, 'A' is 'a'-32 am I in the right path of thinking ?! or there is an easier way ?!
    Make all cases lower with function like tolower(char c) or manually (+- 32) with your own code. The last is better (for learning to program of course).
    Last edited by Carnotter; 04-03-2015 at 05:22 PM.

  12. #12
    Registered User
    Join Date
    Mar 2015
    Location
    BE
    Posts
    66
    Quote Originally Posted by TheShadow View Post
    try it without capital letters it works ,, i'm trying to fix this thing ,, to make it work both of capital and small letters
    also make sure you type '!' after the vote so the loop can stop there
    I tested that too.
    A D H
    ABCEGQ

    I got:
    S1: 1
    S2: 2
    S3: 2

    Shouldn't Score1 be 3?

  13. #13
    Registered User
    Join Date
    Apr 2015
    Location
    Reine, Hazafon, Israel, Israel
    Posts
    11
    Quote Originally Posted by Carnotter View Post
    I tested that too.
    A D H
    ABCEGQ

    I got:
    S1: 1
    S2: 2
    S3: 2

    Shouldn't Score1 be 3?
    im sooo sorry bro my bad ,, I meant small letters excuse me

  14. #14
    Registered User
    Join Date
    Apr 2015
    Location
    Reine, Hazafon, Israel, Israel
    Posts
    11
    I think this is my pre-finished code .. for small and capital letters .. make sure u use '!' after the vote inpute , let me know what u think
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        char a[100];
        char X,Y,Z;
        int score1=0;
        int score2=0;
        int score3=0;
        int test;
    
    
        printf("Welcome to the 2015 elections!\n");
    
    
        printf("Please enter the camps leaders:\n");
        scanf(" %c %c %c", &X , &Y, &Z);
        printf("Please enter the votes:\n");
        scanf(" %s",a);
        for(int i = 0 ; a[i]!='!' ; i++ )
        {
            test= a[i];
            if( ((X<=test)&&(Y>test)) || ( ( (test<Y+32)&&(X+32 <=test) )&&(97<=test) ) )
            {
                score1++;
            }
            if((X<test && test<=Y )|| ( (X+32<test) && (test<=Y+32) ) )
            {
                score2++;
            }
            if((Z<=test && test<='Z' )|| ( (Z+32<test) && (test<='z') ) )
            {
                score3++;
            }
        }
    
    
        printf("score1 is : %d\n",score1);
        printf("score1 is : %d\n",score2);
        printf("score1 is : %d\n",score3);
    
    
    
    
    
    
    
    
    
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Char and Strings [beginner]
    By siggigauti in forum C Programming
    Replies: 2
    Last Post: 01-12-2015, 09:28 AM
  2. Strings: char* or char vectors ?
    By dancp in forum C Programming
    Replies: 10
    Last Post: 07-31-2011, 01:21 PM
  3. strings and char[]
    By grantmitchell in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2008, 06:28 PM
  4. strings and char's
    By mikecompsc in forum C++ Programming
    Replies: 3
    Last Post: 05-21-2007, 01:26 AM
  5. Help with char strings
    By Probose in forum C++ Programming
    Replies: 10
    Last Post: 09-23-2006, 03:38 AM

Tags for this Thread