81.1k views
2 votes
In these days kids are introduced to computers at a very early age and in some schools the dictation test is conducted using computers. The teachers found it a bit difficult to evaluate these tests and they requested the school management to lessen their burden by automating this task. The 12th class students are learning C programming and they took up the task of automating the dictation evaluation. Can you please help them out?

Write a C program to compare 2 strings using strcmp function.

Input Format

Input consists of a two strings.

Constraints

Maximum length of the string is 50 and it contains only alphabets.

Output Format

Refer sample input and output for formatting specifications.

Sample Input 0

Excellent
Excellent
Sample Output 0

It is correct

1 Answer

3 votes

Given below is the C++ program to compare two strings.

Step-by-step explanation:

C++ programming language offers various string functions such as

strcpy (s, s'): copies string s' in string s.

strcat (s, s'): concatenates strings s' to the string s that is string s' is joined to string s at its end.

strlen(s): calculates the length of the string that the number of characters including spaces in the string.

strcmp (s, s'): returns zero if strings s and s' are equal. Hence, This function is used in the below program:

#include<iostream>

#include<cstring>

using namespace std;

int main(){

  • char s1[50], s2[50];
  • cout<< "Enter correct dictation:";
  • cin>>s1;
  • cout<< "Enter testing dictation:";
  • cin>>s2;
  • if(strcmp(s1, s2)==0) { cout<<"It is correct"<<endl; }
  • else { cout<<"It is wrong."<<endl; } }

User DagR
by
7.2k points