简体   繁体   中英

count the number of characters in a string

I'm trying to count the amount of times a character is repeated in a string( can be multiple words ), however, right now i think i'm pretty close to the result but my output is a bit weird:

CODE:

int statistics(){
  char str[122];
  int i=0;
  int count[122] = { 0 };
  printf("Enter a string: ");
  fgets(str,sizeof str, stdin);
  if(fgets(str,sizeof str, stdin) == NULL){
     printf("error");
  }
  size_t size = strlen(str);

  for(int i = 0; i < size; i++) {
      count[(int)str[i]] += 1;
}

for (i = 0; i < 122; i++) {
    if(count[i]>=1){
    printf("The %d. character has %d occurrences.\n", i, count[i]);
    }
  }
  strtok(str,"\n");
  printf("'%s' is %lu characters long\n",str,size-1);
}

eg input: hello

outputs:

The 107. character has 1 occurences.
The 198. character has 1 occurences.
The 201. character has 1 occurences.
The 205. character has 1 occurences.
The 208. character has 1 occurences.
'Hello' is 5 characters long

You are using the current encoding for the index in count . If your encoding is ASCII (the most common encoding) then the character 'H' has the value 72 so you will increase count[72] and and when you print your output you will print it as the 72:th character.

Instead use the loop variable as the counter, and then use the character in str[i] as the index into the counter array.

Something like this

for (size_t i = 0; i < strlen(str); ++i)
{
    if (isprint(str[i])
    {
        printf("Character #%zu is '%c' and the count is %d\n",
               i + 1, str[i], count[str[i]]);
    }
    else
    {
        printf("Character #%zu is 0x%02hhx and the count is %d\n",
               i + 1, str[i], count[str[i]]);
    }
}

By the way, be careful with the input from fgets , it will most likely leave the newline at the end of the string, and you will count that too. I handled in the code above by using isprint to see if the character is "printable" or not.

Also, and more importantly, as you use the character encoding as index in the count array, you need as many elements in it as there are characters in the encoding. With ASCII encoding (the most common and likely what you are using) you need 128 elements to count all possible characters in the ASCII table (not counting "extended ASCII"). So you need to declare your count array with 128 elements. If you don't, then you risk indexing the count array out of bounds, and have undefined behavior .

The reason your output is "weird" is that you are using %d to print character code , instead of %c to print the character itself:

printf("The '%c' (code:%d) character has %d occurrences.\n", i, i, count[i]);

Demo

The 'e' (code:101) character has 1 occurrences.
The 'h' (code:104) character has 1 occurrences.
The 'l' (code:108) character has 2 occurrences.
The 'o' (code:111) character has 1 occurrences.
'hello' is 5 characters long

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM