Visual studio C++ string compare without declaration [duplicate] - c++

This question already has answers here:
C++ Comparison of String Literals
(8 answers)
Closed 7 years ago.
I would like to understand the result of the below code in Microsoft visual studio C/C++ (2012 version). Indeed, when the solution is generated, the result is “1” (True). However, the word “a” is less than the word “z” in ASCII table. So, the result should be “0” (False). Even if I inverse the operation, mean ("z" > "a"). The result is “1”. I tried also this operation ("a" < "z") and ("z" < "a"), the result was “0” for the both
Anyone can explain me what’s happening?
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << ("a" > "z") << endl;
}

Your code is almost the same as if you had written:
const char s1[2] = {'a'};
const char s2[2] = {'z'};
int main()
{
cout << (s1 < s2) << endl;
}
So you can see that your code is actually comparing the addresses of two character arrays.

Related

c++: How do you print to stdout a casted int value? [duplicate]

This question already has answers here:
How to output a character as an integer through cout?
(6 answers)
Convert an int to ASCII character
(11 answers)
Closed 3 years ago.
I have a program that takes a string. Using the check() function that calculate the sum of all the value in the string of integers, I make additional computations, aka ss. The issue comes when I try to convert ss, which is an int, into a char, c.
When I try to print out the newly converted value, nothing prints out on the console, not even an error message.
I have tried using static_cast<char>(ss), and it won't work. Yet when I try to print out the ss value, I get it to print it out.
Source Code
void sum(string input)
{
int s = check(input);
int ss = (s * 9) % 10;
char c = ss;
cout << "val is: " << c << endl;
}
int main()
{
string x = "7992739871";
sum(x);
return 0;
}
Can someone explain what I might be doing wrong and how it can be fixed?
You can use std::to_string() (C++11) but making sure that the value of c is something that can be printable is a better practice.

C++ String Comparison "Good Luck" > "Good Bye" [duplicate]

This question already has answers here:
C++ Comparison of String Literals
(8 answers)
Closed 4 years ago.
I am comparing two strings "Good Luck" > "Good Bye" in c++. Shouldn't the boolean result be true? The 'L' is greater than the 'B' The blank space between the words is one whitespace.
However, I am getting false with run the code.
Do you know why? Below is my code.
#include <iostream>
using namespace std;
int main()
{
bool result = "Good Luck" > "Good Bye!";
cout << result;
return 0;
}
You're comparing char*s, which is just comparing the memory addresses that the strings live at.

How can I compare C-style strings? [duplicate]

This question already has answers here:
Compare equality of char[] in C
(5 answers)
Closed 6 years ago.
The for loop reverses original string 's' and stores it in 'temp.'Temp is printed correctly. After which, s and temp are compared, but the result always shows NO. :(
#include<cstring>
#include <iostream>
using namespace std;
int main()
{
char s[100], temp[100];
cin >> s;
cout << strlen(s);
for(int i=0;i<strlen(s);i++)
{
temp[i]=s[strlen(s)-i];
}
cout << "temp is" << temp;
if(temp==s)
{
cout << "YES";
}
else
{
cout << "NO";
}
return 0;
}
You should use strcmp instead of == because the latter is merely a pointer comparison. Also, '\0' has to be used to end your string. And your current code would actually create an empty string because your string at position strlen(s) contains '\0'.
Then again, you should not work with char arrays in C++ on your own, rather use std::string as already pointed out in comments.

C++ Primer 5th Edition String Exercise asking for two different pieces of code that give the same output? [duplicate]

This question already has answers here:
std::string length() and size() member functions
(4 answers)
Closed 7 years ago.
In this exercise from C++ Primer strings chapter, the directions read:
Write a program to read two strings and report whether the strings are
equal. If not, report which of the two is larger. Now, change the
program to report whether the strings have the same length, and if
not, report which is longer.
To which I wrote:
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
/* Write a program to read two strings and report whether the
strings are equal. If not, report which of the two is larger. Now, change
the program to report whether the strings have the same length, and if
not, report which is longer. */
int main()
{
string line;
string word;
getline(cin, line);
getline(cin, word);
if (line == word) {
cout << "String are equal." << endl;
}
else {
if (line > word)
cout << line << " is longer." << endl;
else {
cout << word << " is longer." << endl;
}
}
return 0;
}
Which seemed to work for the problem. Now for the first example of:
Write a program to read two strings and report whether the strings are
equal. If not, report which of the two is larger.
I changed the comparisons to have .size(), and for this one:
Write a program to read two strings and report whether the strings are
equal. If not, report which of the two is larger.
I removed the .size() for the comparisons. I printed out both size and .length() and I got the same answers, so I was wondering if I am misinterpreting this problem or was it to show that length and size are really the same thing?
Both string::size and string::length are synonyms and return the exact same value.
Reference http://www.cplusplus.com/reference/string/string/length/

how to show the character that come several time in string? [duplicate]

This question already has an answer here:
Counting characters in words in a C++ console application
(1 answer)
Closed 9 years ago.
Is there anyway to show that character s appears 5 times in the string is there any function to sort out the character and than show us that the character appears 5 or 6 times in the string.
#include<iostream>
#include<string>
int main(){
using namespace std;
string a="hello how are you"
//now i want to show the l character appears several time.
//and i need help here.
system("pause");
}
You can use std::count
int lcount = std::count(a.begin(), a.end(), 'l');
Just keep counting using a pointer until nul character is reached and keep increasing an integer on successful comparison.
#include<iostream>
#include<string>
int main(){
using namespace std;
string a="hello how are you";
char *p = &a[0];
int c = 0;
do
{
if(*p == 'l')
c++;
}while(*p++);
cout << c;
}
You can walk over any container, including a string and do what you like with it.
You could count how many instances of each character. You may want to consider ignoring whitespace.
std::string a("hello how are you");
std::map<char,int> count;
for(auto c : a)
{
++count[c];
}