Program to Compare Two Strings in C, C++ (C Plus Plus, CPP) with the flowchart
In this tutorial, we will learn about the followings;
- Flowchart of a program to Compare Two Strings
- Program to Compare Two Strings in C++ (C Plus Plus, CPP)
- Program to Compare Two Strings in C
Flowchart of a program to Compare Two Strings
Coming Soon.
Program to Compare Two Strings in C++ (C Plus Plus, CPP)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#include<iostream> #include<string.h> using namespace std; int main() { char first[30]; char second[30]; int k; int l; int flag=0; cout<<"plz enter first string: "; cin>>first; cout<<"plz enter 2nd string : "; cin>>second; k=0; l=0; while (first[k]!='\0') { k++; } while(second[l]!='\0') { l++; } if(k!=l) { flag=0; } else { for(k=0,l=0;first[k]!='\0',second[l]!='\0';k++,l++) { if(first[k]==second[l]) { flag=1; } } } if(flag==0) { cout<<"Not equal "; } else { cout<<" Equal"<<"/n"; } return 0; } |
Output

Program to Compare Two Strings in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#include<stdio.h> int main() { int a=0; int b=0; int flag=0; char word[20]; char word2[20]; printf("enter first: "); gets(word); printf("enter second"); gets(word2); while (word[a]!='\0') a++; while(word2[b]!='\0') b++; int i=0; while ((i<a)&&(i<b)) { if(word[i]==word2[i]) { i++; continue; } if(word[i]<word2[i]) { flag=-1; break; } if(word[i]>word2[i]) { flag=1; break; } } if(flag==0) printf("both are equal"); if(flag==1) printf("string1 is greater tahn string 2"); if(flag==-1) printf("string1 is less than string 2"); return 0; } |
Output
