Hi!
i trying to take the same value from 2 arrays and then count how many time of it. However, when i compile, it run ok. but it did not print out the value of coutting. I did not know why, i had tried several times to fixing. please help me.

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

int main() {
    char *em[] = {"hello", "bunjour", "ccc"};
    char *e[] = {"hello", "b"};
    int i, j, count = 0;
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++) {
            if (strcmp(em[i], e[j]) == 0) {
                count++;
            }
            if (em[i] == NULL) {
                break;
            }
        }
        if (e[i] == NULL) {
            break;
        }
    }
    printf("count %d\n", count);

    return 0;
}

lines 8 and 9. Why are those loops counting from 0 to 10? Array em only contains 3 trings and array e contains 2 strings.

line 17: array em does not contain a NULL pointer, so line 17 will do nothing.

There are a couple ways to do what you are tring to do. One ways is to add a NULL string at the end of each array so that the program doesn't have to know ahead of time how many strings are in each array. The loops just keep on incrementing until a NULL string is found

The other way is to just hard-code the loop counters with the number of strings in each array, for example for(j = 0; j < 2; j++)

Hi!
i trying to take the same value from 2 arrays and then count how many time of it. However, when i compile, it run ok. but it did not print out the value of coutting. I did not know why, i had tried several times to fixing. please help me.

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

int main() {
    char *em[] = {"hello", "bunjour", "ccc"};
    char *e[] = {"hello", "b"};
    int i, j, count = 0;
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++) {
            if (strcmp(em[i], e[j]) == 0) {
                count++;
            }
            if (em[i] == NULL) {
                break;
            }
        }
        if (e[i] == NULL) {
            break;
        }
    }
    printf("count %d\n", count);

    return 0;
}

Hi,

The below code may help u.Since you have only 3 string in first em variableand 2 string in second e variable then how can you use for loop for 10 times.

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

using namespace std; 

int main() {
    char *em[] = {"hello", "bunjour", "ccc"};
    char *e[] = {"hello", "b"};
    int i, j, count = 0;
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 1; j++) {
            if (strcmp(em[i], e[j]) == 0) {
                count++;
            }
            if (em[i] == NULL) {
                break;
            }
        }
        if (e[i] == NULL) {
            break;
        }
    }
    printf("count %d\n", count);
 
    return 0;
}

Edited 6 Years Ago by Perry31: n/a

@Perry -- the two loops are still incorrect.

Hi Ancient Dragon,

I think this is correct

for (i = 0; i <= 2; i++) {
        for (j = 0; j <= 1; j++) {
This article has been dead for over six months. Start a new discussion instead.