+ 1
C Bug: Character Count
Count the number of characters only if the characters are ('A', 'T', 'C', 'G'), else 'Invalid input'. ========== Example_01 ========== Input: ATTCCGG Output: A 1 T 2 C 2 G 2 ========== Example_02 ========== Input: AKLTCG Output: Invalid input ========== I have the code above which runs for the count but does not tell whether it is an invalid case. Code: https://code.sololearn.com/ca25A24A3A9A ========== Probably, the else if statement in the function, count, went wrong. not sure... Any help, would be highly appreciated... Thanks
4 Respuestas
+ 1
I think your issue may have been with fgets and the that it adds the newline character to the buffer if it is read.
#include <stdio.h>
#include <string.h>
#define MAX_SIZE_STRING 1000
// Not sure if you are only allowed caps but this version will only account for CAP 'A', 'T', 'C', and 'G'
// Sticking with keeping your string at 999 characters plus the '\0'
int main(void){
  int count_of_ATCG[4] = { 0 };
  printf("Enter string to test:\n");
  char test_string[MAX_SIZE_STRING];
  fgets(test_string, MAX_SIZE_STRING, stdin);
  /*  fgets() reads in at most one less than size characters from stream
      and stores them into the buffer pointed to by s.  Reading stops after
      an EOF or a newline.  If a newline is read, it is stored into the buffer.
      A terminating null byte ('\0') is stored after the last character in
      the buffer.
      Your possible issue is that the '\n' (newline) character was being read
  */
  for(int i = 0; i < (strlen(test_string) - 1); i++){
    switch(test_string[i]){
      case 'A' :
        count_of_ATCG[0]++;
        break;
      case 'T' :
        count_of_ATCG[1]++;
        break;
      case 'C' :
        count_of_ATCG[2]++;
        break;
      case 'G' :
        count_of_ATCG[3]++;
        break;
      default:
        printf("Invalid input\n");
        return 0;
    }
  }
  printf("A: %i\nT: %i\nC: %i\nG: %i\n", count_of_ATCG[0], count_of_ATCG[1], count_of_ATCG[2], count_of_ATCG[3]);
  return 0;
}
0
THANKS FOR THE HELP



