Program to Compare Two Strings in C, C++ (C Plus Plus, CPP) with flowchart

Program to Compare Two Strings in C, C++ (C Plus Plus, CPP) with the flowchart

In this tutorial, we will learn about the followings;

  1. Flowchart of a program to Compare Two Strings
  2. Program to Compare Two Strings in C++ (C Plus Plus, CPP) 
  3. 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) 

#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, C++ (C Plus Plus, CPP) with flowchart
Figure Program to Compare Two Strings in C, C++ (C Plus Plus, CPP) with the flowchart.

Program to Compare Two Strings in C

#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

Program to Compare Two Strings in C, C++ (C Plus Plus, CPP) with flowchart
Figure Program to Compare Two Strings in C, C++ (C Plus Plus, CPP) with the flowchart.
Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.