Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

URGENT - Randomize distribution of the letters with C 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Write a function (GeneCount(const char *string )) using a subset of C (see "Restrictions" bellow") to count the number of time the pattern "GTTAC" appears in a list made of a random distribution of the letters G, T, A and C.

Restrictions:
You do NOT have access to string libraries and CANNOT use functions like Stringtok(), strcmp(), etc. Only basic C instructions are allowed (we want to see at how you handle pointers...)

You CAN use any function giving the length (strlen()) or size of a string and functions to copy all or part of a string. You CAN create any additional array, string, list, etc, you think you need to solve the problem.

The function must work with any string containing any number of letters..
----------
Note 1: the input is a nul terminated String. Do NOT use anything else (like a list of the words already tokenized!).
----------

 
Here's how:
Code:
int GeneCount(const char s1[])
{
   char s2[] = "GTTAC";
   int i = 0, j, count = 0;

   while(s1[i] != '\0') {
      for(j=0; s2[j] == s1[i] && s1[i] != '\0' && s2[j] != '\0'; i++, j++)
         ;
      if(s2[j] == '\0') { /* a match found */
         count++;
         i--;
      }
      i++;
   }

return count;
}
[\code]
What was this anyway? Your homework problem? The catch is that you still didn't see me using pointers ;-)

Bye.
Ankan. Please do correct me if I am wrong. :-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top