How to compare strings in C? You deserve to use perform it making use of strcmp function, there is no strcmp role and making use of pointers. Role strcmp is case sensitive and also returns 0 if both the strings space same.

You are watching: C program to compare two strings using pointers

#include #include

int main() char a<100>, b<100>;

printf("Enter a string "); gets(a);

printf("Enter a string "); gets(b);

if (strcmp(a,b) == 0) printf("The strings are equal. "); else printf("The strings are not equal. ");

return 0;

Output of program:


Download to compare Strings program.

C cable comparison program

We can produce a function to compare 2 strings.


int compare_strings(char <>, char <>);  int main() char a<1000>, b<1000>;  printf("Input a string "); gets(a);  printf("Input a string "); gets(b);  if (compare_strings(a, b) == 0) printf("Equal strings. "); else printf("Unequal strings. ");  return 0;

int compare_strings(char a<>, char b<>) int c = 0;  when (a == b) if (a == "" if (a == "" && b == "") return 0; else return -1;

C string comparison making use of pointers

We can make a function to check if 2 strings are comparable or not by utilizing character pointers.


int compare_string(char*, char*);

int main() char first<1000>, second<1000>: int result; printf("Input a string "); gets(first); printf("Input a string "); gets(second); result = compare_string(first, second); if (result == 0) printf("The strings are same. "); else printf("The strings are different. "); return 0;

int compare_string(char *first, char *second) while (*first == *second) *second == "") break; first++; second++;

if (*first == "" && *second == "") return 0; rather return -1;
String compare is a part of pattern matching e.g. Once you press Ctrl+F in a web browser or message editor to find for some text.

See more: Complete Guide To " The Haunting Of Hill House Cliff Notes, The Haunting Of Hill House Summary & Study Guide

C Hello worldPrint IntegerAddition of 2 numbersEven oddAdd, subtract, multiply and divideCheck vowelRoots that quadratic equationLeap year routine in CSum of digitsFactorial routine in CHCF and LCMDecimal to binary in CnCr and nPrAdd n numbersSwapping of 2 numbersReverse a numberPalindrome numberPrint PatternDiamondPrime numbersArmstrong numberArmstrong numbersFibonacci series in CFloyd"s triangle in CPascal triangle in CAddition using pointersMaximum aspect in arrayMinimum element in arrayLinear search in CBinary search in CReverse arrayInsert aspect in arrayDelete facet from arrayMerge arraysBubble type in CInsertion type in CSelection sort in CAdd matricesSubtract matricesTranspose matrixMatrix multiplication in CPrint stringString lengthCompare stringsCopy stringConcatenate stringsReverse wire Palindrome in CDelete vowelsC substringSubsequenceSort a stringRemove spacesChange caseSwap stringsCharacter"s frequencyAnagramsC read fileCopy filesMerge two filesList documents in a directoryDelete fileRandom numbersAdd complex numbersPrint dateGet IP addressShutdown computer

Programming simplified is licensed under a creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.