Question

In: Computer Science

Please write code in c++ using iostream library. Write a function bool cmpr(char * s1, int...

Please write code in c++ using iostream library.

Write a function bool cmpr(char * s1, int SIZE1, char * s2, int SIZE2) that compares two strings.

Input
Input contains two strings. Each string is on a separate line.

example:

aqua

aqua

Output
Output YES if given two strings are the same or NO otherwise.
YES

Solutions

Expert Solution

C++ Program/Source code

Here is the source code of C++ Program to Compare Two Given Strings for Equality. The program output is shown below.

  1. #include<iostream.h>
  2. #include<string.h>
  3. using namespace std;
  4. int main ()
  5. {
  6.     char str1[50], str2[50];
  7.     cout<<"Enter string 1 : ";
  8.     gets(str1);
  9.     cout<<"Enter string 2 : ";
  10.     gets(str2);
  11.     if(strcmp(str1, str2)==0)
  12.         cout << "Strings are equal!";
  13.     else
  14.         cout << "Strings are not equal.";
  15.     return 0;
  16. }

Program Explanation

1. The user is asked to enter two strings and stored in ‘str1’ and ‘str2’.
2. Using an inbuilt function strcmp() under the library string.h, the two strings are compared for equality.
3. The result is then printed if they are equal are not.

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

/*  C++ Program to Compare Two Strings without using strcmp  */

#include<iostream>

#include<string.h>

using namespace std;

int main()

{

     char str1[50],str2[50],i=0,j=0,flag=0;

     cout<<"\nEnter first string :: ";

     gets(str1);

     cout<<"\nEnter Second string :: ";

     gets(str2);

      while(str1[i]!='\0')

      {

        i++;

      }

      while(str2[j]!='\0')

      {

        j++;

      }

     if(i!=j)

     {

        flag=0;

     }

     else

     {

         for(i=0,j=0;str1[i]!='\0',str2[j]!='\0';i++,j++)

         {

             if(str1[i]==str2[j])

             {

                flag=1;

             }

         }

     }

     if(flag==0)

     {

        cout<<"\nStrings are not equal.\n";

     }

     else

     {

        cout<<"\nStrings are equal.\n";

     }

      return 0;

}


Related Solutions

Please, write this code in c++. Using iostream and cstring library. Write a function that will...
Please, write this code in c++. Using iostream and cstring library. Write a function that will delete all words in the given text that meets more that one time. Also note than WORD is sequence of letters sepereated by whitespace. Note. The program have to use pointer. Input: First line contains one line that is not longer than 1000 symbols with whitespaces. Each word is not longer than 30 symbols. Output: Formatted text. example: input: Can you can the can...
Please, write code in c++. Using iostream and cstring library Write a function that will find...
Please, write code in c++. Using iostream and cstring library Write a function that will find and return most recent word in the given text. The prototype of the function have to be the following void mostRecent(char *text,char *word) In char *word your function have to return the most recent word that occurce in the text. Your program have to be not case-sensitive(ignore case - "Can" and "CAN" are the same words) Also note than WORD is sequence of letters...
Write a function int strlen(char s1[]) which returns the length of the char array s1.
Write a function int strlen(char s1[]) which returns the length of the char array s1.
Please, write code in c++. Using iostream library A chessboard pattern is a pattern that satisfies...
Please, write code in c++. Using iostream library A chessboard pattern is a pattern that satisfies the following conditions: • The pattern has a rectangular shape. • The pattern contains only the characters '.' (a dot) and 'X' (an uppercase letter X). • No two symbols that are horizontally or vertically adjacent are the same. • The symbol in the lower left corner of the pattern is '.' (a dot). You are given two numbers. N is a number of...
Please, write code in c++. Using iostream and cstring library. Your friend is the person who...
Please, write code in c++. Using iostream and cstring library. Your friend is the person who does not like any limitations in the life. And when you said to him that it is totally impossible to work with integer numbers bigger than 4 294 967 296 in C++ he blamed you in time-wasting during the university study.So to prove that you hadn't waste 2 months of your life studying C++ in university you have to solve this issue. Your task...
Please, write code in c++. Using iostream and cstring library. You given a text.Your task is...
Please, write code in c++. Using iostream and cstring library. You given a text.Your task is to write a function that will find the longest sequence of digits inside. Note that the output have to be presened just like in sample. Note. The program have to use pointer. Input: First line contains one line that is not longer than 1000. Output: The longest sequence of numbers.All numbers are positive and integers. example: input: 101 fdvnjfkv njfkvn fjkvn jffdvfdvfd2010 output: 2010
Please write code in c++ using iostream library. Also you can use any string library. Create...
Please write code in c++ using iostream library. Also you can use any string library. Create structure plane with the following: 1)plane's ID[int type] 2) location[char*] 3) departure time[char*] Your task is to find the plane with the smallest departure time for the selected city. Pointer, dynamic array and structures have to be used in this code. Input: NOTE: It's just an example of input, you should write code generally for any inputs. First line contains n(0 < n <...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
Please, write code in c++. Using iostream library Most modern text editors are able to give...
Please, write code in c++. Using iostream library Most modern text editors are able to give some statistics about the text they are editing. One nice statistic is the average word length in the text. A word is a maximal continuous sequence of letters ('a'-'z', 'A'-'Z'). Words can be separated by spaces, digits, and punctuation marks. The average word length is the sum of all the words' lengths divided by the total number of words. For example, in the text...
Please, write code in c++. Using iostream and cstring library Use pointers! You given a text.Your...
Please, write code in c++. Using iostream and cstring library Use pointers! You given a text.Your task is to write a function that will find the longest sequence of digits inside. Note that the output have to be presened just like in sample. Note. The program have to use pointer. Input: First line contains one line that is not longer than 1000. Output: The longest sequence of numbers.All numbers are positive and integers. Samples: № INPUT OUTPUT 1 This is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT