Comparing Strings:
Strings are compared to find out whether they are equivalent or unequal. Whenever they are unequal, one string is "more than" or "less than" the other. Determination of "greater" and "less" are made up with the ASCII codes of characters. In case of letters, this is equal to alphabetical order, with the one apparently strange exception which all uppercase letters are "less than" the lower-case letters. This is true since the uppercase letters contain ASCII codes 65 through 90 for A through Z, whereas lower-case a through z are symbolized by 97 through 122. Therefore, "ZEBRA" would be considered to be less than "apple" by such C functions.
The ANSI C library comprises of functions for two types of string comparisons: comparing two whole strings, and comparing a certain number of characters in the two strings.Comparing Two Entire Strings:
The function strcmp() compares the two strings character by character. Its prototype is
int strcmp(char *str1, char *str2);
The arguments str1 and str2 are the pointers to strings being compared. The functions return values are given in table shown below. Following Listing expalins strcmp().
The values returned by strcmp().
Using strcmp() to compare strings. /* The strcmp() function. */ #include <stdio.h> #include <string.h> main() { char str1[80], str2[80]; int x; while (1) { /* Input two strings. */ printf("\n\nInput the first string, a blank to exit: "); gets(str1); if ( strlen(str1) == 0 ) break; printf("\nInput the second string: "); gets(str2); /* Compare them and display the result. */ x = strcmp(str1, str2); printf("\nstrcmp(%s,%s) returns %d", str1, str2, x); } return(0); }
Output:
Input the first string, a blank to exit: First string Input the second string: Second string strcmp(First string,Second string) returns -1 Input the first string, a blank to exit: test string Input the second string: test string strcmp(test string,test string) returns 0 Input the first string, a blank to exit: zebra Input the second string: aardvark strcmp(zebra,aardvark) returns 1 Input the first string, a blank to exit:Comparing Partial Strings:
The library function strncmp() compares a particular number of characters of one string to the other string. Its prototype is,
int strncmp(char *str1, char *str2, size_t n);
The function strncmp() compares n characters of str2 to str1. Comparison proceeds until n characters have been compared or end of the str1 has been reached. The technique of comparison and return values similar as for strcmp(). The comparison is case-sensitive.
=> Comparing parts of strings with the strncmp(): /* The strncmp() function. */ #include <stdio.h> #include[Sigma]>=tring.h> char str1[] = "The first string."; char str2[] = "The second string."; main() { size_t n, x; puts(str1); puts(str2); while (1) { puts("\n\nEnter number of characters to compare, 0 to exit."); scanf("%d", &n); if (n <= 0) break; x = strncmp(str1, str2, n); printf("\nComparing %d characters, strncmp() returns %d.", n, x); } return(0); } Output:
The first string. The second string. Enter number of characters to compare, 0 to exit. 3 Comparing 3 characters, strncmp() returns .©] Enter number of characters to compare, 0 to exit. 6 Comparing 6 characters, strncmp() returns -1. Enter number of characters to compare, 0 to exit. 0
Latest technology based Programming Languages Online Tutoring Assistance
Tutors, at the www.tutorsglobe.com, take pledge to provide full satisfaction and assurance in Programming Languages help via online tutoring. Students are getting 100% satisfaction by online tutors across the globe. Here you can get homework help for Programming Languages, project ideas and tutorials. We provide email based Programming Languages help. You can join us to ask queries 24x7 with live, experienced and qualified online tutors specialized in Programming Languages. Through Online Tutoring, you would be able to complete your homework or assignments at your home. Tutors at the TutorsGlobe are committed to provide the best quality online tutoring assistance for Programming Languages Homework help and assignment help services. They use their experience, as they have solved thousands of the Programming Languages assignments, which may help you to solve your complex issues of Programming Languages. TutorsGlobe assure for the best quality compliance to your homework. Compromise with quality is not in our dictionary. If we feel that we are not able to provide the homework help as per the deadline or given instruction by the student, we refund the money of the student without any delay.
Theory and lecture notes of Introduction to Linear Systems all along with the key concepts of linear algebra, How linear systems occur, Triangular matrices, back substitution, Gaussian Elimination. Tutorsglobe offers homework help, assignment help and tutor’s assistance on Introduction to Linear Systems.
Environmental Management Systems Assignment Help work with the priority to ensure that all your queries bne answered on time.
Faradays laws of electrolysis tutorial all along with the key concepts of Faraday's First Law of Electrolysis, Faraday's Second Law of Electrolysis, Electrolysis of water, Electro-Refining Operations, Applications of Electrolysis
From the horizontal oscillator, Horizontal frequency 15,625Hz is delivered to the horizontal driver transistor BD115. The collector voltage is delivered by Rt and primary of driver transformer. Emitter is being earthed.
Surface Tension of Solutions tutorial all along with the key concepts of Adsorption on Solids, Adsorption of Gases by Solids, Adsorption Isotherms and Langmuir Adsorption Isotherm
Avail notable Geophysics Assignment Help at affordable prices and put an end to sleepless nights by scoring A++!
automatic frequency control stage automatically controls the variations in horizontal frequency. therefore it is known as automatic frequency control.
Theory and lecture notes of Intercepts, Zeros, and Solutions all along with the key concepts of Complex Numbers, Solving Equations Algebraically, Linear Equations and Modeling. Tutorsglobe offers homework help, assignment help and tutor’s assistance on Intercepts, Zeros, and Solutions.
economic efficiency and key concepts of economic efficiency, transfer price, price ceiling, technical efficiency, invisible hand and bankruptcy, answering questions by managerial economics tutors, homework help, assignment help.
Isolation Mechanisms tutorial all along with the key concepts of Morphological species concept, Biological species concept, Pre-zygotic, Temporal isolation, Mechanical isolation, Post-zygotic isolation and Genetics of isolation barriers
tutorsglobe.com significance of financial management assignment help-homework help by online financial management tutors
Theory and lecture notes on AC Fundamental Principles and Phasors all along with the key concepts of phasors, angular frequency, cyclic frequency, period, peak voltage, average value and complex phase plane. Tutorsglobe offers homework help, assignment help and tutor’s assistance on AC Fundamental Principles and Phasors.
tutorsglobe.com adaptations of ornithophilous flowers assignment help-homework help by online ornithophily tutors
structures of crystals tutorial all along with the key concepts of definition of crystal, types of crystals, ionic crystals, covalent crystals, molecular crystals and metallic crystals
www.tutorsglobe.com offers reactions of alcohols homework help, reactions of alcohols assignment help, online tutoring assistance, organic chemistry solutions by online qualified tutor's help.
1949092
Questions Asked
3689
Tutors
1479135
Questions Answered
Start Excelling in your courses, Ask an Expert and get answers for your homework and assignments!!