IHYPRESS PROGRAMMING
Tutorials and C programs with code and output for beginners
c programming
HOME | ASP | C | CSS | GNUPLOT | HTML | JAVASCRIPT | PERL | PHP | PYTHON | RUBY | SVG
CStrings : Comparison
<08.04>
/* Comparing two strings */
#include <stdio.h> #include <string.h> #define LENGTH 50 int main (void) { char city1[50], city2[50]; int comp; printf ("Comparison of two city names\n"); printf ("============================\n\n"); printf ("Enter the name of the first city (<50 letters): "); fgets (city1, LENGTH, stdin); city1[strlen(city1)-1] = '\0'; printf ("Enter the name of the second city (<50 letters): "); fgets (city2, LENGTH, stdin); city2[strlen(city2)-1] = '\0'; /* strncmp compares two strings */ comp = strncmp (city1, city2, LENGTH); if (comp < 0) printf ("\n\n%s < %s.\n", city1, city2); else if (comp == 0) printf ("\n%s = %s.", city1, city2); else printf ("\n%s > %s.", city1, city2); return (0); }
Hergestellt in Deutschland / Made in Germany
Comparison of two city names ============================ Enter the name of the first city (<50 letters): Vancouver Enter the name of the second city (<50 letters): Halifax Vancouver > Halifax.
COPYRIGHT © 2015-2024 IHY PRESS Frankfurt am Main 60329 Deutschland