#include <stdio.h>int longest_segment(char *dna_sequence) { int max_length = 1; int current_length = 1; char previous_char = dna_sequence[0]; for (int i = 1; dna_sequence[i] != '\0'; i++) { if (dna_sequence[i] == previous_char) { current_length++; } else { if (current_length > max_length) { max_length = current_length; } current_length = 1; } previous_char = dna_sequence[i]; } if (current_length > max_length) { max_length = current_length; } return max_length;}int main() { char dna_sequence[101]; printf(" "); scanf("%s", dna_sequence); int result = longest_segment(dna_sequence); printf(" %d\n", result); return 0;}
Question
#include <stdio.h>int longest_segment(char *dna_sequence) { int max_length = 1; int current_length = 1; char previous_char = dna_sequence[0]; for (int i = 1; dna_sequence[i] != '\0'; i++) { if (dna_sequence[i] == previous_char) { current_length++; } else { if (current_length > max_length) { max_length = current_length; } current_length = 1; } previous_char = dna_sequence[i]; } if (current_length > max_length) { max_length = current_length; } return max_length;}int main() { char dna_sequence[101]; printf(" "); scanf("%s", dna_sequence); int result = longest_segment(dna_sequence); printf(" %d\n", result); return 0;}
Solution
This is a C program that finds the longest segment of repeating characters in a DNA sequence. The DNA sequence is inputted by the user. The function longest_segment iterates through the DNA sequence and keeps track of the longest segment of repeating characters. The result is then printed out.
Here's a step-by-step explanation:
-
The
longest_segmentfunction is defined. It takes a stringdna_sequenceas an argument. -
Two integer variables
max_lengthandcurrent_lengthare initialized to 1.max_lengthwill store the length of the longest segment of repeating characters, andcurrent_lengthwill store the length of the current segment of repeating characters. -
The character variable
previous_charis initialized to the first character ofdna_sequence. -
A for loop is used to iterate through
dna_sequence. If the current character is the same asprevious_char,current_lengthis incremented. If it's different,current_lengthis compared tomax_length, and if it's greater,max_lengthis updated tocurrent_length.current_lengthis then reset to 1, andprevious_charis updated to the current character. -
After the loop,
current_lengthis compared tomax_lengthone last time to account for the case where the longest segment of repeating characters is at the end ofdna_sequence. -
max_lengthis returned. -
In the
mainfunction, a stringdna_sequenceof length 101 is declared. The user is prompted to input a DNA sequence, which is stored indna_sequence. -
longest_segmentis called withdna_sequenceas the argument, and the result is stored in the integer variableresult. -
resultis printed out, and the program ends.
Similar Questions
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.