-2

I have an assignment where I need to find substrings in an array I have.

this is my array:

char DNA[] = {'A', 'G', 'C', 'G', 'G', 'G', 'A', 'C', 'C', 'G', 'T', 'C', 
      'C', 'C', 'G', 'A', 'C', 'A', 'T', 'T', 'G', 'A', 'T', 'G', 
      'A', 'A', 'G', 'G', 'G', 'T', 'C', 'A', 'T', 'A', 'G', 'A', 
      'C', 'C', 'C', 'A', 'A', 'T', 'A', 'C', 'G', 'C', 'C', 'A', 
      'C', 'C', 'A', 'C', 'C', 'C', 'C', 'A', 'A', 'G', 'T', 'T', 
      'T', 'T', 'C', 'C', 'T', 'G', 'T', 'G', 'T', 'C', 'T', 'T', 
      'C', 'C', 'A', 'T', 'T', 'G', 'A', 'G', 'T', 'A', 'G', 'A', 
      'T', 'T', 'G', 'A', 'C', 'A', 'C', 'T', 'C', 'C', 'C', 'A', 
      'G', 'A', 'T', 'G', '\0'};

The user will enter a string such as CAT and I'll need to make a program such that the printf statement will display the elements of where CAT is found.

I tried using the strstr function but this only gives me the first occurence in the array. However, if CAT appears more than once, it will not print that statement out, so im wondering how should I do this?

This is what I have so far:

char input [100];

char DNA[] = {'A', 'G', 'C', 'G', 'G', 'G', 'A', 'C', 'C', 'G', 'T', 'C', 
      'C', 'C', 'G', 'A', 'C', 'A', 'T', 'T', 'G', 'A', 'T', 'G', 
      'A', 'A', 'G', 'G', 'G', 'T', 'C', 'A', 'T', 'A', 'G', 'A', 
      'C', 'C', 'C', 'A', 'A', 'T', 'A', 'C', 'G', 'C', 'C', 'A', 
      'C', 'C', 'A', 'C', 'C', 'C', 'C', 'A', 'A', 'G', 'T', 'T', 
      'T', 'T', 'C', 'C', 'T', 'G', 'T', 'G', 'T', 'C', 'T', 'T', 
      'C', 'C', 'A', 'T', 'T', 'G', 'A', 'G', 'T', 'A', 'G', 'A', 
      'T', 'T', 'G', 'A', 'C', 'A', 'C', 'T', 'C', 'C', 'C', 'A', 
      'G', 'A', 'T', 'G', '\0'};

printf("enter string ");
scanf("%s", &input);

char *find;

find = strstr(DNA, input);

if (find != NULL)
{
    printf("the string is found at element %d\n", (find - DNA)+1);
}

If I type CAT, the program will say its at element 17, but there is another CAT at element 74.

2
  • 5
    Hint: if you want something to happen more than once, do it in a loop. Commented Oct 20, 2015 at 19:25
  • Possible duplicate of Delete duplicate string in array Commented Nov 8, 2016 at 2:52

4 Answers 4

1

A while loop is sufficient for this task.

#include <stdio.h>

int main(void)
{
    char input[100];

    char DNA[] = {
                    'A', 'G', 'C', 'G', 'G', 'G', 'A', 'C', 'C', 'G', 'T', 'C', 
                    'C', 'C', 'G', 'A', 'C', 'A', 'T', 'T', 'G', 'A', 'T', 'G', 
                    'A', 'A', 'G', 'G', 'G', 'T', 'C', 'A', 'T', 'A', 'G', 'A', 
                    'C', 'C', 'C', 'A', 'A', 'T', 'A', 'C', 'G', 'C', 'C', 'A', 
                    'C', 'C', 'A', 'C', 'C', 'C', 'C', 'A', 'A', 'G', 'T', 'T', 
                    'T', 'T', 'C', 'C', 'T', 'G', 'T', 'G', 'T', 'C', 'T', 'T', 
                    'C', 'C', 'A', 'T', 'T', 'G', 'A', 'G', 'T', 'A', 'G', 'A', 
                    'T', 'T', 'G', 'A', 'C', 'A', 'C', 'T', 'C', 'C', 'C', 'A', 
                    'G', 'A', 'T', 'G', '\0'
                 };

    printf("enter string : ");

    scanf("%s", input);


    char *ptr = DNA;

    while( (ptr = strstr(ptr,input)) != NULL )
    {
        printf("the string is found at element %d\n", (ptr-DNA)+1);
        ptr++;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

By the way you should check scanf for errors and %d si wrong, should be %ld because (ptr-DNA)+1) is long int not int..
I made a Mistake, sorry. :)
0

One approach would be to change your if statement to a while loop, modifying find on each iteration. Note that, just like DNA, find also references a character pointer - just at a different location in the string. Therefore, the rest of the string can be accessed simply by using ++find as your starting point.

while(find != NULL){
    printf("the string is found at element %d\n", (find - DNA)+1);
    find = strstr(++find, input);
}

This produces a fairly crude output, but it gives you something to work with.

Comments

0

Well, the strstr() reference documentation says (emphasis mine):

Finds the first occurrence of the null-terminated byte string pointed to by substr in the null-terminated byte string pointed to by str. The terminating null characters are not compared. The behavior is undefined if either str or substr is not a pointer to a null-terminated byte string.

So to find further occurrences, call strstr() repeatedly in a loop with the result pointer + 1, until it returns NULL.


As from your request in comment, something like should find all occurrences of the sequence:

char *find = NULL;
char *start = DNA;

do {
    find = strstr(start, input);

    if (find != NULL) {
        printf("the string is found at element %d\n", (find - start)+1);
        start = find + 1;
    }
} while(find != NULL);

3 Comments

could you give me possible pseudo code? I'm a beginner coder so I'm a bit confused still with what you mean. LIke I was thinking of a loop, except I couldn't seem to get it correct.
thanks for the sample code. now in my assignment, it states the * operator is considered a "wildcard" term which can be any letter in the array (A,G,T,C) . how do i set the * letter to equal any of those 4 letters, so that if the user enters CA*, it can be considered either CAT, CAG, CAC, CAA?
@RayChen That's a different question. I'm not (and Stack Overflow isn't) your personal help-desk. See [How to ask]( stackoverflow.com/help/asking) for future questions.
0

I think this is What you need:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void checkString(char *string1, char *string2){
    char *s1, *s2, *s3;

    size_t lenstring1 = strlen(string1);
    size_t lenstring2 = strlen(string2);

    if (lenstring2 < 1){
        printf("There is no substring found");
        exit(1);
    }

    size_t i=0,j=0;
    int found=0;

    s1 = string1;
    s2 = string2;


    for(i = 0; i < lenstring1; i++){
        if(*s1 == *s2){
            s3 = s1;
            for(j = 0;j < lenstring2;j++){
                if(*s3 == *s2){
                  s3++;s2++;
                }else{
                    break;
                }
            }

            s2 = string2;
            if(j == strlen(string2)){
                found = 1;
                printf("%s found at index : %zu\n",string2,i+1);
              }
          }
        s1++;
    }

    if(found == 0){
        printf("No match Found");
    }
}

int main(void){
    char string1[] = {'A', 'G', 'C', 'G', 'G', 'G', 'A', 'C', 'C', 'G', 'T', 'C',
      'C', 'C', 'G', 'A', 'C', 'A', 'T', 'T', 'G', 'A', 'T', 'G',
      'A', 'A', 'G', 'G', 'G', 'T', 'C', 'A', 'T', 'A', 'G', 'A',
      'C', 'C', 'C', 'A', 'A', 'T', 'A', 'C', 'G', 'C', 'C', 'A',
      'C', 'C', 'A', 'C', 'C', 'C', 'C', 'A', 'A', 'G', 'T', 'T',
      'T', 'T', 'C', 'C', 'T', 'G', 'T', 'G', 'T', 'C', 'T', 'T',
      'C', 'C', 'A', 'T', 'T', 'G', 'A', 'G', 'T', 'A', 'G', 'A',
      'T', 'T', 'G', 'A', 'C', 'A', 'C', 'T', 'C', 'C', 'C', 'A',

      'G', 'A', 'T', 'G', '\0'};
    char string2[] = "CAT";

    checkString(string1, string2);
    return 0;
}

Output:

CAT found at index : 17
CAT found at index : 31
CAT found at index : 74

2 Comments

thanks michi, although i have no idea how to interpret your code. (im beginner so my knowledge is pretty limited.
That isn't a reason to post the same question again without making any effort.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.