0
How do you compare more than 2 or multiple strings in C language?
Comparing multiple strings in C
4 odpowiedzi
+ 2
Like what for example?
+ 1
//by using list
list a = ["a","b"]
list b = ["a", "c"]
for(var i in a){ for(var i in b) { if(a[i] ==b[i]) return true;} return false;}
+ 1
#include <stdio.h>
#include <string.h>
int main() {
char str[50] = "Hello";
if(strstr(str,"He") != NULL) printf("T"); else printf("F");
return 0;
}
//Keep learning & happy coding :D
0
int testing(char *test, char *test1){
char name[6];
int b=0;
int c = 2;
int results = 0;
for(int a = 0; a<=(strlen(test1)); a++){
if(test1[a] == ' ' || name[b-1] == ' '){
name[b] = '\0';
if(c%2 == 0){
if(strcmp(test,name) == 0){
results = 1;
}else{
results = 0;
break;
}
}
c++;
b = 0;
}
name[b] = test1[a];
b++;
}
return results;
}
Here's a code a wrote for comparing 2 or more strings, please add a space at the end of the string you wanna compare.
Below is the example code you can run and understand how this works.
#include <stdio.h>
#include <string.h>
int testing(char *test, char *test1){
char name[6];
int b=0;
int c = 2;
int results = 0;
for(int a = 0; a<=(strlen(test1)); a++){
if(test1[a] == ' ' || name[b-1] == ' '){
name[b] = '\0';
if(c%2 == 0){
if(strcmp(test,name) == 0){
results = 1;
}else{
results = 0;
break;
}
}
c++;
b = 0;
}
name[b] = test1[a];
b++;
}
return results;
}
int main(){
printf("%d",testing("hello", "hello hello hello hello hello "));
}