C++ String Comparison
Write a program that asks the user to enter two names and store them in string objects.. It should then report whether they are the same or not, ignoring case, To help accomplish its task, it should use two functions in addition to main(). The functions are: StringUppercaseIt(string s) boolean sameString(string s1, string s2)
The sameString function, which receives the two strings to be compared, will need to call uppercaseIt for each of them before testing if they are the same. The uppercaseIt function should use a loop so that it can call the toupper function for every character in the string it receives before returning it back to the sameString function.
Learning to code? Follow my C++ tutorials for beginners. I do a lot of C++ programming challenges and C++ projects as part of my homework – every day!
If you are learning to program, nothing beats solving real programming exercises and coding challenges.
So don’t forget to subscribe, as I release new programming videos every day!
Tag: compare string c++, C++ string.Compare, PavolC++, C++ strings, C++ For loops, C++ tutorial for beginners, C++ exercises, C (Programming Language), String, C String Handling
Xem thêm: https://icongnghe.org/category/review
Nguồn: https://icongnghe.org
14 Bình luận. Leave new
How do you compare say s1"Your Name Here"; s2"your name here"; now make or convert s2 to s1. Converted s2"Your Name Here";. If you can thanx in advance.
what the fn toupper() do?
could you do a switch case once the comparison was done? i want to do a switch case for like for if north or n go to a north place, then if south or s then go to south place?
meow!
Two names ????? please i have a problem with collection of strings compare and sort them… Like sorting by video category
Man, the best i could find in this vast world of internet, there are no spanish youtubers who explains this case, they use c style string to explain it (pretty tired of it,, i am a c++ student) you got my like buddy!
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <iomanip>
#include <string>
#include <vector>
#include <ctype.h>
#include <algorithm>
using namespace std;
string upperCase(string z){
for(int i=0; i<z.length(); i++){
z[i] = toupper(z[i]);
}
return z;
}
void sameString(string a, string b){
if(upperCase(a) == upperCase(b)){
cout << "Same" << endl;
}else{
cout << "Different" << endl;
}
}
int main() {
string in, in2;
cout << "- Same Name Testing Machine -" << endl << endl;
cout << "Enter the first name here ";
getline(cin, in);
cout << "Enter the second name here ";
getline(cin, in2);
sameString(in, in2);
return 0;
}
How can you compare 2 strings, one element at a time? For instance, if you have 2 strings "0011" and "0010" and you want to perform an OR bitwise operation on it?
Very Nice Tutorial, Thanks Man
hi, how do I compare an array of strings?
wonderful video…helped much for my Intro to C++
Your videos really help a lot. Thanks for uploading.
How about removing the first occurrence of the second c-String argument from the first c-string. And returns the index position in the first c string where the second Beas found otherwise return -1 ?
Wow! You are the Man.
Thank you.