kshane
kshane

Reputation: 23

Is it possible to compare strings from the same variable in C?

I wanted to try to compare characters of strings from the same variable so I could just use for when comparing several other strings.

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

int main () {

    char this_string[2][10] = {"Jason", "jason"},
    string1[6] = "Jason",
    string2[6] = "jason",
    ans[6];

    int x;

    for (x=0; x<5; x++) {
        if (string1[x]!=string2[x]) {
            strcpy(ans, string2);
            ans[x] = '-';
        }   
    }

    printf("%s\n", ans);

}

The output would be "-ason". But is there a way to do the same with this_string?

Upvotes: 1

Views: 64

Answers (1)

Rustam
Rustam

Reputation: 6515

yes do like this :

 for (x=0; x<5; x++) {
        if (this_string[0][x]!=this_string[1][x]) {
            strcpy(ans, this_string[1]);
            ans[x] = '-';
        }   
    }

Upvotes: 3

Related Questions