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
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
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.
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; }
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.
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 }
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.)
A little inaccuracy saves tons of explanation. - H.H. Munro
Oh yeah, Thanks. Is fine, THEN let me rephrase my words to "How about this code I've found"
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
How about writing the code yourself. You won't learn much by just "finding" some random code from some random place."How about this code I've found"
Step 1
Step 2Code:while ( (ch=getchar()) != EOF ) { printf("Found character %c (code=%d)\n", ch, ch ); }
The lesson is, start really small and make small changes.Code:while ( (ch=getchar()) != EOF ) { if ( ch == 'A' ) { printf("Found character %c (code=%d)\n", ch, ch ); } }
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.
Thank you guys for helping me, I solve the problem alrdy