C Program Returns Unexpected Numbers -


I'm sorry if this question is really dumb, but I started learning c and no one knew I was Was able to help. I should calculate the number of letters in stdin and say how many times each one was used, it is also expected that we use a counter and fred method.

EDIT: Igor helped me with my answer and the program is now almost everything is right. Little mistakes are still there: only the first 10 characters are identified and they are calculated, Who are ignored And if the input should be bigger than 10 or not, then it does not work.

I have commented on the changes that I have made in the code so that others can follow my mistakes.

I have the code: #include & lt; Stdio.h & gt; # Include & lt; ctype.h & gt; Int main () {Four lessons [10]; Int bar was used [26]; // I started the array, but did not set the value of 0. Int bar usage [26] = {0}; // This line corrected the issue with getting random numbers; textLength = 0; Char * ptr; While (feeds (& amp; text, size (four), 10, stdin)) {ptr = & amp; Text [0]; While (1) {if (alpha (* ptr)) {textLength ++; * Ptr = tolower (* ptr); // Original was only Tiller (* ptr); Which is worthless time [((four) * ptr) - 'A'] ++; } If (* ptr == '\ n') goto end; // Original here only one '=' / ** ptr = '\ 0'; * / If (ptr == & lesson [10]) goto end; // both on PTR ++; }} End printf ("number of characters:% 20d \ n", textLength); Int i; For (I = 0; i 0) {char thechar = (four) 'a' + i; printf ("letter% c% 5d time ( S) was used. \ N ", thechar, timezone [i]);}} return 0;}

that does not print expected results, which actually prints Is:

  test test test test test m number of letters: 9 letters were used 2 times letter g was used 1 time letter i used 1 time (s) The letter was done n Was used 1 time (s) The letter was used in 1 time (s) The letter T3 time (s) was used   

The changes used to output Much more meaningful has been made but still some mistakes.

next time -wall Compile your code with and -VITRR to note some of your mistakes.

You have to start the array:

  int timesUsed [26] = {0};   

When you do not do this, there are random values ​​in it and the count is not correct.

This statement has no effect:

  tolower (* ptr);   

Do this instead:

  * ptr = tolower (* ptr);   

and in C = is not equal to == which checks for equality, so if you change the statement: < / P>

  if (* ptr == '\ n') goto end; / ** ptr = '\ 0'; * / If (ptr == & lesson [10]) goto end;    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -