number of times number appears in array

This is a discussion on number of times number appears in array within the C Programming forums, part of the General Programming Boards category; Hi everyone. I am trying to write a simple function that counts the number of times a number appears in ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    number of times number appears in array

    Hi everyone. I am trying to write a simple function that counts the number of times a number appears in a given string. The function keep returning a very large negative number. Am I perhaps making a mistake with where I am declaring variables?
    Code:
    1. #include <stdio.h>
    2. #include <string.h>
    3. char line[100];
    4. int count(int number, char array[], int length);
    5. int search_num;
    6. int array_length;
    7. int main()
    8. {
    9. printf("Enter a series of numbers: ");
    10. fgets(line, sizeof(line), stdin);
    11. printf("Enter a number to check occurence of: ");
    12. scanf("%d", &search_num);
    13. array_length = strlen(line);
    14. printf("The number %d appeared %d times in this array\n",
    15. search_num, count(search_num, line, array_length));
    16. return 0;
    17. }
    18. int count(int number, char array[], int length)
    19. {
    20. int i;
    21. int number_count;
    22. for (i = 0; i < length; i++)
    23. {
    24. if (array[i] == number)
    25. number_count++;
    26. else
    27. continue;
    28. }
    29. return (number_count);
    30. }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    What is the value of number_count the first time you increment it?

  3. #3
    Registered User dariyoosh's Avatar
    Join Date
    Nov 2012
    Location
    Iran / France
    Posts
    33
    ... I am trying to write a simple function that counts the number of times a number appears in a given string ...
    I think you may want to change the way you search for the specific number in your string value, at the lines 24, 25 in your code
    Code:
    if (array[i] == number)
         number_count++;
    allows to compare only one digit each time as each digit in this context being represented as a char. So for example if I want to search 87, inside a loop, I detect 8 with i as the value of the loop counter and I will detect 7 at the next iteration (i + 1). In other words, two digits (or more) numbers cannot be detected as a whole, in the way you're comparing and searching inside the string. A more appropriate solution consist of providing a for example comma delimited string, where numbers are separated by a delimiters. Then define a function which splits the string into several tokens (therefore each token a number), putting those tokens converted to number inside an array of integers and then search inside that array for the specific number.


    Regards,
    Dariyoosh

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Take the number you want to search for, as a string instead of a number. Then search for it in the text string with strstr(). That's what strstr() was made to do.

    You already have the included file you need for this.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    Thank you for all the help. My first time posting here and the responses were great.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-23-2012, 11:31 PM
  2. Replies: 4
    Last Post: 11-05-2012, 02:29 PM
  3. Replies: 25
    Last Post: 10-31-2009, 01:45 AM
  4. Replies: 16
    Last Post: 11-11-2008, 11:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21