Created
March 9, 2015 16:33
-
-
Save tudorconstantin/3c508752383d2ce42941 to your computer and use it in GitHub Desktop.
c string comparison for dragos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| int main(void) | |
| { | |
| int ctr; | |
| int i = 0; | |
| int found = 0; | |
| char name[3] = "Bob"; | |
| char name1[3]; | |
| while(name[i]) | |
| { | |
| printf(" %d", name[i++]); | |
| } | |
| printf("Give me a name pls\n"); | |
| scanf(" %s", name1); | |
| while(name1[i]) | |
| { | |
| printf(" %d", name1[i++]); | |
| } | |
| for(ctr = 0;ctr < 3; ctr++) | |
| { | |
| //am pus printf-ul asta ca sa vad caracterul curent name[i] si mi-am dat seama ca in varianta ta, ai folosit i ca si contor, cand de fapt aici e ctr care itereaza | |
| printf("Name:%c|Name1:%c|\n", name[ctr], name1[ctr]); | |
| //nu aveai acolade la if - e instructiune valida, dar iti lua doar found=1 cand era conditia true, iar breakul ti-l executa tot timpul, indiferent ca e true sau false conditia. | |
| if(name[ctr] == name1[ctr]){ | |
| found = 1; | |
| break; | |
| } | |
| } | |
| if(found) | |
| { | |
| printf("Hello, %s", name1); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment