User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,553 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,493 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1742 | Replies: 12
Reply
Join Date: Oct 2007
Posts: 9
Reputation: Exsiss is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Exsiss Exsiss is offline Offline
Newbie Poster

Counting an inputted letter in an inputted string using getchar()

  #1  
Oct 16th, 2007
I need to make the program:

Write a program to ask for a string and a single character. Read in the string using getchar(). Read in the character using scanf(). Count and report the number of occurrences of the character in the string. Add a brief comment at the beginning of the file describing what the program does.

An interactive session with the program should look like (note the space at the end of the prompts, that the string appears on a separate line, indented and quoted, and that the character is printed within single quotes; sample user input is in red):

Enter any string: I could have entered anything, but this is what I typed.

Enter any character: t

In the following string:

"I could have entered anything, but this is what I typed."

The character 't' appears 6 times

This is what I got so far, but I'm completely stuck now because I keep getting errors:
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4.  
  5. int main (int argc, char**argv)
  6. {
  7. int i;
  8. int letterCount;
  9. char LetterToMatch[1];
  10. char string[220];
  11.  
  12. letterCount = 0;
  13. for(i = 0; i < strlen(string); i++)
  14. {
  15. if (string[i] = getchar[LetterToMatch])
  16. letterCount++;
  17.  
  18. if (string[i] == '\n')
  19. {
  20. break;
  21. }
  22. }
  23.  
  24.  
  25. printf("Enter any string: ");
  26. scanf (" %s", &string);
  27. printf("Enter any character: ");
  28. scanf (" %s", &LetterToMatch);
  29. printf("In the following string:\n");
  30. printf(" \"%s\"\n", string);
  31. printf("The character '%c" appears %d time%s\n",
  32. LetterToMatch, letterCount, letterCount == 1 ?"":"s");
  33.  
  34. return(0);
  35. }
  36.  

I'm not even positive I have the right syntax, but here are the errors I'm getting for this code:

15:subscripted value is neither array nor pointer
26: warning: char format, different type arg (arg 2)
28: warning: char format, different type arg (arg 2)
31: parse error before "appears"
31: stray '\' in program
31:55: warning: multi-line string literals are deprecated
32:54: warning: multi-line string literals are deprecated
32:54: missing terminating " character
31:55: possible start of unterminated string literal

Please keep in mind that I am a beginner so please try and explain in a way a beginner would understand. Thanks!
Last edited by Ancient Dragon : Oct 16th, 2007 at 7:41 am. Reason: add line numbers
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Counting an inputted letter in an inputted string using getchar()

  #2  
Oct 16th, 2007
getchar uses round parentheses not square brackets to hold the parameters.

you don't need the & address operator on line 26 because arrays are ALWAYS passed by address.

line 28 has a couple problems: if you only want one character then use "%c" instead of "%s". and it has the same problem as above on line 26.

line 31: The double quote immediately after '%c should be a single quote. The rest of the errors will be fixed too when you correct that.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Posts: 9
Reputation: Exsiss is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Exsiss Exsiss is offline Offline
Newbie Poster

Re: Counting an inputted letter in an inputted string using getchar()

  #3  
Oct 16th, 2007
I fixed those things, but I'm still getting errors:
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4.  
  5. int main (int argc, char**argv)
  6. {
  7. int i;
  8. int letterCount;
  9. char LetterToMatch[1];
  10. char string[220];
  11.  
  12. letterCount = 0;
  13. for(i = 0; i < strlen(string); i++)
  14. {
  15. if (string[i] = getchar(LetterToMatch))
  16. letterCount++;
  17.  
  18. if (string[i] == '\n')
  19. {
  20. break;
  21. }
  22. }
  23.  
  24.  
  25. printf("Enter any string: ");
  26. scanf (" %c", &string);
  27. printf("Enter any character: ");
  28. scanf (" %c", &LetterToMatch);
  29. printf("In the following string:\n");
  30. printf(" \"%s\"\n", string);
  31. printf("The character '%c' appears %d time%s\n",
  32. LetterToMatch, letterCount, letterCount == 1 ?"":"s");
  33.  
  34. return(0);
  35. }

The errors I'm getting:

15:54: macro "getchar" passed 1 arguments, but takes just 0
In function 'main':
15: warning: assignment makes integer from pointer without cast
15: warning: suggest parentheses around assignment used as truth value
26: warning: char format, different type arg (arg 2)
28: warning: char format, different type arg (arg 2)
32: warning: int format, pointer arg (arg 2)
literal
Last edited by Exsiss : Oct 16th, 2007 at 10:44 am.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Counting an inputted letter in an inputted string using getchar()

  #4  
Oct 16th, 2007
what have YOU tried to fix the problems? Look at the error messages, they tell you what lines are incorrect. Then look up the function in your textbook or online and see why the compiler says you are wrong.

line 13 is wrong -- what is the value of string when it first enters that loop ? Hint: its value is just some random junk because you didn't initialize it to anything. And even if you did initialize it that line would still be wrong.
Last edited by Ancient Dragon : Oct 16th, 2007 at 10:51 am.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Oct 2007
Posts: 9
Reputation: Exsiss is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Exsiss Exsiss is offline Offline
Newbie Poster

Re: Counting an inputted letter in an inputted string using getchar()

  #5  
Oct 16th, 2007
Thanks for the help on line 13. I guess I need to look up initializing strings some more and understand it a little better.

I see the errors, I just don't know what they mean. For instance does "char format, different type arg" always mean the letter that comes after "%" (same with int format)? and I don't even know what a truth value or a cast is. and "15:54: macro "getchar" passed 1 arguments, but takes just 0" I don't even understand at all.

And they are not things I can find in my book or online (because I don't even know what to be searching for).
Last edited by Exsiss : Oct 16th, 2007 at 11:18 am.
Reply With Quote  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Counting an inputted letter in an inputted string using getchar()

  #6  
Oct 16th, 2007
Why is LetterToMatch defined as an array of one char? Why not just define it as a char and it won't be a pointer, correcting some of the errors.
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Join Date: Oct 2007
Posts: 9
Reputation: Exsiss is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Exsiss Exsiss is offline Offline
Newbie Poster

Re: Counting an inputted letter in an inputted string using getchar()

  #7  
Oct 16th, 2007
Originally Posted by WaltP View Post
Why is LetterToMatch defined as an array of one char? Why not just define it as a char and it won't be a pointer, correcting some of the errors.



Do you mean making line 9 "char LetterToMatch[];" instead of "char LetterToMatch[1]"?

Because when I do that it says I have an array size missing in "LetterToMatch".

Sorry if this all sounds stupid, I'm having trouble grasping the C language.
Reply With Quote  
Join Date: Oct 2007
Posts: 9
Reputation: Exsiss is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Exsiss Exsiss is offline Offline
Newbie Poster

Re: Counting an inputted letter in an inputted string using getchar()

  #8  
Oct 16th, 2007
Okay, I don't know if it was a very good mistake or if something clicked in my brain but I was able to figure something out, my code is now:

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4.  
  5. int main (int argc, char**argv)
  6. {
  7. int i;
  8. int letterCount;
  9. char LetterToMatch;
  10. char string[30];
  11.  
  12. letterCount = 0;
  13. while ( (i = getchar() ) != EOF)
  14. {
  15. if ( i >= 'LetterToMatch')
  16. letterCount++;
  17. }
  18.  
  19.  
  20. printf("Enter any string: ");
  21. scanf (" %c", string);
  22. printf("Enter any character: ");
  23. scanf (" %c", &LetterToMatch);
  24. printf("In the following string:\n");
  25. printf(" \"%s\"\n", string);
  26. printf("The character '%c' appears %d time%s\n",
  27. LetterToMatch, letterCount, letterCount == 1 ?"":"s");
  28.  
  29. return(0);
  30. }
  31.  

And now the only error I have is

[quote}
15:28: warning: character constant too long
[/quote]

what does it mean when it says '15:28:'? Is it still referring to lines? And can someone tell me what "character constant too long" means?

Also, the 'while ( (i = getchar() ) != EOF)' I got from the book. does anyone know what EOF is supposed mean?

Also, I tried running the program anyway and when I did a blank line was given where I can type and send, but nothing happens and I can't get out of it.

Thanks
Last edited by Exsiss : Oct 16th, 2007 at 3:15 pm.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,515
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 489
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Counting an inputted letter in an inputted string using getchar()

  #9  
Oct 16th, 2007
>if ( i >= 'LetterToMatch')
I'm going to go out on a limb and guess that you meant:
if ( i == LetterToMatch )
LetterToMatch is a variable, and you're checking to see if i matches it, correct? Also, if you're going to test against LetterToMatch, you need to make sure that it's actually been set to something.

>does anyone know what EOF is supposed mean?
It's supposed to mean end-of-file, or "you've reached the end of the data".
Last edited by Narue : Oct 16th, 2007 at 3:16 pm.
I'm here to prove you wrong.
Reply With Quote  
Join Date: Oct 2007
Posts: 9
Reputation: Exsiss is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Exsiss Exsiss is offline Offline
Newbie Poster

Re: Counting an inputted letter in an inputted string using getchar()

  #10  
Oct 16th, 2007
Thank you Narue, you have been extremely helpful! I fixed the if statement and it changed my error to '22:28:' instead of '15:28:' so it must have done something (although i don't know what haha).

>if you'r going to test against LetterToMatch, you need to make sure that it's actually been set to something.
I'm guessing that because you said this that LetterToMatch is not set to anything? I'm so confused, I know how to do this code without the scanf() but not knowing what the letter is is throwing me off.

Should I have a #define LetterToMatch at the beginning?

Another thing I'm having trouble grasping is what exactly (i = getchar() ) is telling the program (again, I'm having troubles understanding getchar, this might be due to the fact I've been up practically all night trying to figure these programs out, :/). Maybe if I understand what (i = getchar() ) is doing I will understand this a little better. (namely I'm confused to what i is even representing...)

Also, do you think I need the '!= EOF' in line 20?

Sorry about all the questions, this is just really frustrating me.

Edit: I also just changed line 22 to "if ( string[i] == 'LetterToMatch')" and I tried running the program anyway and when I did it did ask me to enter any string, but then when I did and sent it it said "Enter any character: In the following screen:" and then went down a new line and is just blank, and I can type anything, but it won't do anything, and it won't let me out of the program.
Last edited by Exsiss : Oct 16th, 2007 at 3:41 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 5:20 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC