C Program To Compare Two Strings Using strcmp Function
#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
if (strcmp(a,b) == 0)
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}
OUTPUT:
Enter the first string
Compare
Enter the second string
Compare
Entered strings are equal.
C Program To Compare Two Strings Without Using strcmp Function
#include <stdio.h>
int compare_strings(char [], char []);
int main()
{
int flag;
char a[1000], b[1000];
printf("Input first string\n");
gets(a);
printf("Input second string\n");
gets(b);
flag = compare_strings(a, b);
if (flag == 0)
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}
int compare_strings(char a[], char b[])
{
int c = 0;
while (a[c] == b[c]) {
if (a[c] == '\0' || b[c] == '\0')
break;
c++;
}
if (a[c] == '\0' && b[c] == '\0')
return 0;
else
return -1;
}
OUTPUT:
Input first string
ComparingStrings
Input second string
ComparingStrings
Entered strings are equal.
C Program To Compare Two Strings Using Pointers
#include <stdio.h>
int compare_string(char*, char*);
int main()
{
char first[1000], second[1000], result;
printf("Input first string\n");
gets(first);
printf("Input second string\n");
gets(second);
result = compare_string(first, second);
if (result == 0)
printf("Both strings are same.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}
int compare_string(char *first, char *second) {
while (*first == *second) {
if (*first == '\0' || *second == '\0')
break;
first++;
second++;
}
if (*first == '\0' && *second == '\0')
return 0;
else
return -1;
}
OUTPUT:
Input first string
CholbeNaa
Input second string
Cholbe
Entered strings are not equal.
ALSO READ:
-- Program To Print First Ten(10) Natural Numbers.
-- Program To Print Fibonacci Series Upto 100.
-- Program To Check Whether A Number Is Prime Or Not.
-- Program To Compute The Reverse Of A Number.
-- Program To Print A Semicolon Without Using A Semicolon.
-- Program To Display Function Without Using The Main Function.
-- Program To Print Any Print Statement Without Using Semicolon.
-- Program To Display Its Own Source Code As Its Output.
-- Program To Swap Two Strings Using strcpy() Function.
-- Program to accept a string and print the reverse of the given string.
-- Program To Get IP Address.
-- Program To Accept An Integer & Reverse It.
-- Program To Convert Given Number Of Days To A Measure Of Time Given In Years, Weeks & Days.
-- Program To Illustrate The Concept Of Unions.
-- Program To Find The Size Of A Union.
... Back To C++ Programs Index
... Back To C++ FAQ's Index
... Back To C Programs Index
... Back To C FAQ's Index
Comments
Post a Comment
Please share your opinions and suggestions or your experience in the comments section. This way we can all help each other...
Experienced guys can share their resumes at admin@interview-made-easy.com