• Register
1 vote
25 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,260 points

Related questions

0 votes
1 answer 40 views
40 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 17 views
17 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 13k points
0 votes
1 answer 51 views
51 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 17 views
17 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 13k points
0 votes
1 answer 14 views
14 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 9.9k points
0 votes
1 answer 28 views
28 views
Problem: Does strcmp() work through each index of two arrays of characters and if the ASCII value is smaller at an identical index of two strings, that string is there alphabetically greater and therefore a 0 or 1 or 2 is returned?
asked Apr 11 ummesalma 29.2k points
0 votes
1 answer 18 views
18 views
Problem Please help me, how i compare two strings in c++
asked Mar 27 Subhendu 4.5k points
0 votes
1 answer 5 views
5 views
Problem: How to concatenate two strings in c without using strcat?
asked 3 days ago Junia Phoebe 15.3k points
0 votes
1 answer 18 views
18 views
Problem: I am learning Windows Forms Applications and I want to compare two strings, one that is taken from user in a textbox and other that I already have hardcoded in my program. I have used == and .Equals but both of these return false even if the input is same. I don&rsquo;t ... .Visible = true; label1.Text = "The name is Angel"; } else { label1.Visible = true; label1.Text = "Wrong Input"; } }
asked Oct 16, 2020 Code Learner 9.9k points
0 votes
1 answer 13 views
13 views asked Feb 4 Akhil k 1.3k points