0

So I may be attacking this the wrong way; I was learning a bit of Javascript and wrote a program that will find a string contained in another string and store the matches in an array (sorry if that didn't make too much sense, seeing my C program should help). I tried to translate my program to C, but it doesn't work; I think my issue is data types:

 int main () {
    char text[58];
    strcpy(text, "I am James, James the Great, a pretty cool guy named James"); 
    char store[16];
    char name[6] = "James";
    for (int i = 0; i <= 16; i++) {
        if (text[i] == 'J') {
            for (int k = i; k < i + 6; k++) {
                store[k] = text[i];
                }
        }
    }
    int a = sizeof(store) / sizeof(store[0]);
    for (int b = 0; b < a; b++) {
        /* This should print out the values stored in the store array */
        printf("%d", store[b]);
    }
    return 0;
}

My expected result would be something like:

JamesJamesJames

Instead I get:

10000747474747474-6374747474

So I'm assuming it's doing exactly what I told it to do, just not storing the letters into the array as letters, a bit confusing. Is there a better way to do this?

This is the javascript code I was trying to translate:

var text = "I am James, James the Great, a pretty cool guy named James";

var myName = "James";
var hits =[];
for (i = 0; i < text.length; i++) {
  if (text[i] === "J") {
      for (var j = i; j < i + myName.length ; j++) {
          hits.push(text[j]);
      }
  }
}
if (hits.length === 0) {
    console.log("Your name wasn't found!");
} else{
    console.log(hits);
}
2
  • 1
    0) "%d" --> "%c" Commented Apr 5, 2015 at 16:38
  • is there some problem with simply using the system function: strstr() ? Commented Apr 5, 2015 at 20:44

3 Answers 3

1

You are going to break store[] with this line

for (int k = i; k < i + 6; k++) {

because you are writing to the same index as you found the text in text[]. You also loop one too many times (6)

int ind = 0;
for (int k = 0; k < 5; k++) {
    store[ind++] = text[i+k];
}

Then your next statement doesn't tell you anything about the data you collected

int a = sizeof(store) / sizeof(store[0]);

Just remove it, and print ind characters from store[].

Sign up to request clarification or add additional context in comments.

Comments

0

You made following mistakes.

for (int i = 0; i <= 16; i++)

You iterate by '16' but this isn't proper length of text.

for (int k = i; k < i + 6; k++)

So you iterate by "i, i+1, i+2, i+3, i+4, i+5, i+6" here is 7 chars, "James" have 6.

store[k] = text[i];

You aren't changing "i" value so text[i] always return 'J'. It's better to use savePoistion variable so do I in the following code.

int a = sizeof(store) / sizeof(store[0]);
    for (int b = 0; b < a; b++) {
        /* This should print out the values stored in the store array */
        printf("%d", store[b]);
    }

I have no idea what are you trying to do here. It's really unnecessary.

printf("%d", store[b]);

"%d" means that you are printing integers so don't be surprised that your output was numbers. Just simply printf("%s\n", store);

This should look like this

int main () {
    char text[58];
    strcpy(text, "I am James, James the Great, a pretty cool guy named James");
    char store[20];
    int len = strlen(text);
    int savePosition = 0;

    int i, k;
    for (i = 0; i < len; i++) {
        if (text[i] == 'J') {
            for ( k = i; k < i + 5; k++) {
                store[savePosition] = text[k];
                ++savePosition;
                }
        }
    }
    store[savePosition] = '\0';
    printf("%s\n", store);
    return 0;
}

1 Comment

Yeah, I think I had it as %s earlier but it accidentally turned into a %d. Thank you very much, great answer and really easy to follow!
0
int main (void) {
    char text[] = "I am James, James the Great, a pretty cool guy named James"; 
    char name[] = "James";
    char store[16] = {0};
    int store_index = 0;
    for (int i = 0; i < strlen(text); i++) {
        if (text[i] == 'J') {
            for (int k = i; k < i + strlen(name); k++) {
                store[store_index++] = text[k];
            }
        }
    }
    if(strlen(store)==0) {
        printf("Your name wasn't found!\n");
    } else {
        printf("%s\n", store);
    }
    return 0;
}

rewrite version.

int main (void) {
    char text[] = "I am James, James the Great, a pretty cool guy named James"; 
    char name[] = "James";
    char store[sizeof(text)];
    char *p = strstr(text, name);
    if(p == NULL) {
        printf("Your name wasn't found!\n");
    } else {
        int len = strlen(name);
        int index = 0;
        do {
            memcpy(store + index, name, len);
            index += len;
            p = strstr(p + len, name);
        } while(p != NULL);
        store[index] = 0;

        printf("%s\n", store);
    }
    return 0;
}

4 Comments

just beat me with a better answer, but I suggest for (int i = 0; i <= strlen(text)-strlen(name); i++)
@WeatherVane yes, this code has problem. but I was trying to translate javascript version.
Great code, but a little hard to correlate with the javascript lol. But this definitely will help me get the feel of C a little better, thanks!
@steelcowboy dagi12's code has same problem that Weather Vane has been pointed out by his comment.

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.