• Register
1 vote
21 views

Problem:

Well, comparing two strings lengths in a c++ program is quite easy. I wonder if I can compare two strings c++ without using strcmp! Do you have any idea guys? Please, share with me.
Thanks for your concern.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

As far as your problem is concern, yes! there are some other ways to compare two strings in c++. We can use Relational operators and std:: Compare() function. Hence I love the second option, I am going with it. 

#include <iostream>
using namespace std; 
void compareString(string str1, string str2){ 
    int n = str1.compare(str2); 
    if (n != 0) 
        cout << str1 << " is not as like as "
             << str2 << endl; 
    if (n > 0) 
        cout << str1 << " is larger than "
             << str2 << endl; 
    else
        cout << str2 << " is larger than "
             << str1 << endl; 
}
int main(){ 
    string str1("Hello-World"); 
    string str2("World-Hi"); 
    compareString(str1, str2); 
    return 0; 
}

Here I declared two different string into two differents variable str1 and str2. And then pass these string through a function compareString() as parameters. The program should throw an output

Hello-World is not as like as World-Hi
World-Hi is larger than Hello-World


Leave me a kudos if the problem is solved. Thanks!

13 9 6
94,240 points

Related questions

0 votes
1 answer 16 views
16 views
I want to know how to compare two strings without using strcmp.
asked Sep 28, 2020 Daniel Anderson 4k points
0 votes
1 answer 30 views
30 views
1 I'm trying to compare the first character of two different strings (and so on) to form a new string based on those results. This is what I've tried using, however its comparing every element of each list to each other. def compare(a,b): s = "" for x in a: for y in b: if x == y: s+=str(x) else: s+=str(y) It seems like such a simple question but I'm stuck.
asked Oct 25, 2020 psandprop 2.4k points
0 votes
1 answer 9 views
9 views
Problem: In this C++ series, until now, we have mostly discussed numeric arrays in C++. In, How will discuss the manipulation with character arrays which we simply call &ldquo;Strings&rdquo;? Character array is mostly a C-style string that C++ supports. In ... tutorial, how will discuss both the types of strings as well as the difference between them and about programming each type in C++?
asked Dec 12, 2020 Mashhoodch 9.1k points
0 votes
1 answer 8 views
8 views
Problem: Hello friends, I am learning to work with strings in C++ and want to know how to concatenate two strings in C++. I have no background in programming so expecting a detailed answer.
asked Oct 12, 2020 Code Learner 5.8k points
0 votes
1 answer 14 views
14 views
Problem: How to compare two Strings in Java using the compareTo() method, equals() method or == operator. The compareTo() method compares two strings. The comparison is based on the Unicode value of each character in the strings. give me code of these examples.
asked Nov 3, 2020 Mashhoodch 9.1k points
0 votes
1 answer 5 views
5 views asked Feb 4 Akhil k 1.3k points
0 votes
1 answer 10 views
10 views
Problem: Hello! I just came across a programming problem in which the programmer has to write a program that counts the number of the same characters in two strings. I am puzzled about how to achieve the count of the same characters. I want help from you in finding the solution. Thanks!
asked Jan 8 Code Learner 5.8k points
0 votes
1 answer 28 views
28 views
I am trobuleing to find the length of array in c without using sizeof.
asked Oct 3, 2020 Daniel Anderson 4k points
0 votes
1 answer 8 views
8 views asked Jan 28 khanboy 2.1k points
0 votes
1 answer 9 views
9 views
Problem Hey, I want to know how to concatenate two strings using operator overloading. Can someone help me learn this?
asked Jan 28 chris jordan 2.4k points