Write a C program to compare two strings using pointers.
#include <stdio.h>
#include <string.h>
int main() {
char str1[256], str2[256];
int i, flag = 0;
/* get the first string from the user */
printf("Enter your first input string:");
fgets(str1, 256, stdin);
str1[strlen(str1) - 1] = '\0';
/* get the second string from the user */
printf("Enter your second input string:");
fgets(str2, 256, stdin);
str2[strlen(str2) - 1] = '\0';
/* checks whether given two strings are same or not */
for (i = 0; i <= strlen(str1); i++) {
if (str1[i] != str2[i]) {
flag = 1;
break;
}
}
/* print the output */
if (flag) {
printf("Given two strings are not same\n");
} else {
printf("Given two strings are same\n");
}
return 0;
}
#include <string.h>
int main() {
char str1[256], str2[256];
int i, flag = 0;
/* get the first string from the user */
printf("Enter your first input string:");
fgets(str1, 256, stdin);
str1[strlen(str1) - 1] = '\0';
/* get the second string from the user */
printf("Enter your second input string:");
fgets(str2, 256, stdin);
str2[strlen(str2) - 1] = '\0';
/* checks whether given two strings are same or not */
for (i = 0; i <= strlen(str1); i++) {
if (str1[i] != str2[i]) {
flag = 1;
break;
}
}
/* print the output */
if (flag) {
printf("Given two strings are not same\n");
} else {
printf("Given two strings are same\n");
}
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your first input string:Micheal Jackson
Enter your second input string:Micheal Jack
Given two strings are not same
Enter your first input string:Micheal Jackson
Enter your second input string:Micheal Jack
Given two strings are not same
No comments:
Post a Comment