Thread: Please, anyone help me with this question. I'm really stuck and i need your guys help

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    5

    Question Please, anyone help me with this question. I'm really stuck and i need your guys help

    1. Write a program for you to key in 10 capital letters from keyboard, and show how many times of letter A you have keyed in on your monitor. This question is about C. Thank you. Please help me

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,489
    Start with the getchar() function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    5
    Is this correct? :
    Code:
    #include <stdio.h>
    
    int main()
    {
       char str[1000], ch;
       int i, number = 0;
    
       printf("Enter a string: ");
       gets(str);
    
       printf("Enter letter A: ");
       scanf("%c",&ch);
    
       for(i = 0; str[i] != '\0'; ++i)
       {
           if(ch == str[i])
               ++number;
       }
    
       printf("Number of %c = %d", ch, number);
    
       return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,447
    No, definitely not.

    First never, Never, NEVER use gets(), this function can never be used safely and has actually been removed from the language in the current standard.

    Second why are retrieving a string and then trying to retrieve a single character? Your assignment doesn't mention using a string, or only checking how many times "A" is contained within a string.

  5. #5
    Registered User
    Join Date
    Nov 2018
    Posts
    5
    okay thanks guys for the explanation. How about I wrote like this:
    Code:
     #include<stdio.h>
     int main() 
    { 
         int c ; 
         int count ;  
         while ( ( c = getchar() ) != EOF )   if ( ( c >= 'A' ) && ( c <= 'Z' ) ||        ( c >= 'a' ) && ( c <= 'z' ) )  
         count ++ ;
         printf( "%d letters\n" , count ) ; 
         return 0 
    }

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    631
    Ignoring the incredibly bizarre spacing, your program will count how many alphabetic characters were entered. But that's not what the question asked. (And, TBH, I don't actually believe you wrote that.)
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  7. #7
    Registered User
    Join Date
    Nov 2018
    Posts
    5
    Oh yeah, Thanks. Is fine, THEN let me rephrase my words to "How about this code I've found"

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    3,675
    Quote Originally Posted by Qwell View Post
    Oh yeah, Thanks. Is fine, THEN let me rephrase my words to "How about this code I've found"
    It is not yet what you need; I would start by indenting properly and decide how you are going to change the code.

    Edit: If you do not yet know what changes; list the input and output that are expected. And, compare that to what the program outputs, instead.

    Tim S.
    Last edited by stahta01; 11-27-2018 at 10:32 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,447
    "How about this code I've found"
    How about writing the code yourself. You won't learn much by just "finding" some random code from some random place.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,489
    Step 1
    Code:
    while ( (ch=getchar()) != EOF ) {
        printf("Found character %c (code=%d)\n", ch, ch );
    }
    Step 2
    Code:
    while ( (ch=getchar()) != EOF ) {
        if ( ch == 'A' ) {
            printf("Found character %c (code=%d)\n", ch, ch );
        }
    }
    The lesson is, start really small and make small changes.

    When things go wrong, don't just reach for the delete key and write something else.

    Study what you saw (compiler messages, bad output, whatever). Use that information to improve your ability to predict the future outcome of what you write.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Nov 2018
    Posts
    5
    Thank you guys for helping me, I solve the problem alrdy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hello guys, I have a question regarding Arrays
    By Cdd101 in forum C Programming
    Replies: 60
    Last Post: 10-05-2013, 12:35 AM
  2. Hey guys, back again with a question
    By Velocity in forum C++ Programming
    Replies: 10
    Last Post: 10-19-2008, 02:27 PM
  3. OK. new easy question for you guys.
    By arnis in forum C++ Programming
    Replies: 8
    Last Post: 07-17-2003, 09:52 PM
  4. Ok. sorry to bother you guys with a newb question.
    By arnis in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2003, 11:23 AM
  5. Need to ask you guys a Question....
    By Halo in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-03-2003, 01:38 AM

Tags for this Thread