Invalid use of void expression in strcmp - c++

I am looking to check if two string are permutations of each other. I am using the following code :
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
void sort(char *str)
{
char temp;
for(int i=0;i<strlen(str);i++)
{
if(str[i]>str[i+1])
{
temp=str[i];
str[i]=str[i+1];
str[i+1]=temp;
}
}
}
int main()
{
char string1[10],string2[10];
int val;
cout<<"Enter first string";
gets(string1);
cout<<"Enter second string";
gets(string2);
val = strcmp(sort(string1),sort(string2));
if(val==0)
{
cout<<"Same strings"<<endl;
}
else
{
cout<<"Different Strings"<<endl;
}
return 0;
}
But I am getting a "invalid use of void expression error" at the strcmp line. How do I fix this ?
Thanks

It looks like you want to compare strings after sorting. Assuming your sort function does the right thing, you need to compare the strings after sorting them.
sort(string1);
sort(string2);
val = strcmp(string1, string2);
The reason for the error is that your sort function returns void. So you are effectively passing void arguments to strcmp. And that can't work.
The way to do this in C++ would be to use std::string, and call std::sort.
std::string string1, string2;
std::cout << "Enter first string";
std::cin >> string1;
std::cout << "Enter second string";
std::cin >> string2;
std::sort(string1.begin(), string1.end());
std::sort(string2.begin(), string2.end());
bool val = string1 == string2;

sort returns nothing (void), so its return value cannot serve as parameter to strcmp.
sort(string1);
sort(string2);
val = strcmp(string1, string2);

You can't strcmp the void returns from sort(). You want to sort() first and then strcmp the sorted strings, something like:
sort(string1);
sort(string2);
val = strcmp(string1, string2);

Related

Conversion of string to string *

I'm facing an error while assigning the reverse of a string to another string.
[Error] incompatible types in assignment of 'char*' to char[20]
This is my code
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char str[20],str2[20];
int length;
cout<<"Enter the string\n";
cin>>str;
length=strlen(str);
str2=strrev(str);
if(str==str2)
{
cout<<"Palindrome";
}
else{
cout<<"Not a palindrome";
}
return 1;
}
Any explanation on what i've done wrong would be really helpful.
Cheers
As other people have recommended, a simple solution is to use std::string and not char [20], and then you can call method reverse() to do the job.
Here is my simple code:
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string str, str2;
std::cout << " Please Enter The String : \n";
std::cin >> str;
str2 = str;
reverse(str.begin(), str.end());
if( str == str2 )
std::cout << "Palindrome";
else
std::cout << "Not a palindrome";
return 0;
}
I have tested and verified that the code works.
strrev is a nonstandard C function it makes the reversal in-place. So the assignment makes no sense. strcpy + strcmp might have been appropriate.
Since you tagged this c++ You really want to use std::string to make things more concise.
#include<string>
#include<iostream>
#include<algorithm>
int main(){
std::string in1, palindrome;
std::cin >> in1;
palindrome = in1;
// reverse palindrome with std::reverse
// compare with == for like contents.
}

Two strings are anagrams or not question quitted with code 11 on C++ Clion

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
void LowerCharacters(string& text);
void OutPutEachLetter(string text, int* ptr);
void comparePointers();
int main() {
string text1, text2; bool isAnagrams = true;
int *ptr1=NULL, *ptr2=NULL;
cout<<"Please enter first string ";
getline(cin,text1);
cout<<"Please enter second string ";
getline(cin,text2);
LowerCharacters(text1);
LowerCharacters(text2);
OutPutEachLetter(text1, ptr1);
OutPutEachLetter(text2, ptr2);
for(int i =0; i<26;i++) {
if (ptr1[i] != ptr2[i]) {
isAnagrams = false;
}
}
if(isAnagrams)
cout<<"These two strings are anagrams";
else
cout<<"These two strings are not anagrams";
delete []ptr1; delete []ptr2;
ptr1=NULL; ptr2=NULL;
}
void LowerCharacters(string& text) {
for (int i = 0; i < text.length(); i++) {
text[i]=tolower(text[i]);
}
}
void OutPutEachLetter(string text, int* ptr){
ptr = new int[26];
*ptr = {0};
for(int i =0;i<text.length()-1;i++)
{
ptr[text[i]-'a']++;
}
for(int j=0;j<26;j++){
if(ptr[j]!=0){
cout<<ptr[j]<<"\t"<<char(j+'a')<<endl;
}
}
}
Above was my code. I thought it would have worked but it did not. it printed the ptr1 successfully but never printed the ptr2. I want to know why... Any help will be appreciated!
I am kind of new to pointer stuff, I just wanted to try my best to use the pointer in my code so that I can practice more. I know that I can totally just create two static arrays in the main part and then write the array in my function OutputEachLetter as an argument and use the pass as a reference like int &arr. However, I really want to know why is my current code wrong and why am I not allowed to use this kind of code.
You could just do a little effort by adding algorithm library plus you're using C++, take some benefits of it. Just use the transform() for conversion of strings into lower case, and sort them then see if they're equal or not.
Everything could be done in a simplified way.
Consider the written program:
#include <iostream>
#include <algorithm>
int main(void) {
std::string str1, str2;
std::cout << "Enter the first string: ";
std::getline(std::cin, str1);
std::cout << "Enter the second string: ";
std::getline(std::cin, str2);
/* --- this --- */
transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
transform(str2.begin(), str2.end(), str2.begin(), ::tolower);
std::sort(str1.begin(), str1.end());
std::sort(str2.begin(), str2.end());
if (str1 == str2)
std::cout << "Both strings are anagrams";
else
std::cout << "No anagrams";
std::cout << std::endl;
return 0;
}

Convert char array to a string with cin.getline(.)

hi guys so my question is how to convert a char array to a string. here is my code:
#include<iostream>
using namespace std;
int main()
{
while (true) {
char lol[128];
cout << "you say >> ";
cin.getline(lol,256);
cout << lol << endl;;
}
return 0;
}
so I want to convert lol to a string variable like "stringedChar" (if thats even english lol)
so I can do stuff like:
string badwords[2] = {"frick","stupid"};
for (int counter = 0; counter < 2;counter++) {
if(strigedChar == badwords[counter]) {
bool isKicked = true;
cout << "Inappropriate message!\n";
}
}
Sorry im just a c++ begginer lol
Do something like this :
as char lol[128];
into string like: std::string str(lol);
Line : cin.getline(lol,256); <--> should be changed to cin.getline(lol,128)
Just invoke std::getline() on a std::string object instead of messing about with a char array, and use std::set<std::string> for badwords as testing set membership is trivial:
#include <iostream>
#include <set>
#include <string>
static std::set<std::string> badwords{
"frick",
"stupid"
};
int main() {
std::string line;
while (std::getline(std::cin, line)) {
if (badwords.count(line) != 0) {
std::cout << "Inappropriate message!\n";
}
}
return 0;
}
Note that this tests whether the entire line is equal to any element of the set, not that the line contains any element of the set, but your code appears to be attempting to do the former anyway.
First off, you have a mistake in your code. You are allocating an array of 128 chars, but you are telling cin.getline() that you allocated 256 chars. So you have a buffer overflow waiting to happen.
That said, std::string has constructors that accept char[] data as input, eg:
#include <iostream>
using namespace std;
int main()
{
while (true) {
char lol[128];
cout << "you say >> ";
cin.getline(lol, 128);
string s(lol, cin.gcount());
cout << s << endl;;
}
return 0;
}
However, you really should use std::getline() instead, which populates a std::string instead of a char[]:
#include <iostream>
#include <string>
using namespace std;
int main()
{
while (true) {
string lol;
cout << "you say >> ";
getline(cin, lol);
cout << lol << endl;;
}
return 0;
}

How do you remove multiple characters in the strings that are held in a vector, in c++?

I want to delete characters of a string that are in a vector, starting from an index inputted by the user up until the end of that string. For example if in index 0, my vector has the string "hello" and index 1 has the string, "goodbye", I want to erase the characters "llo" in the first string and "dbye" in the second string. So the result will be "he" in index 0 and "goo" in index 1. In my code that I am posting, I did not add the part of getting input from the user for the index. But just pretend, it is index 4 and beyond. How would I do this? Thank you.
I tried putting a '\0' character at the index that I want to start the deletion at, but that does not work.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
int maxSize;
cin >> maxSize;
string usrInput;
vector<string> myArray;
for(int i = 0; i < maxSize; i++)
{
cin >> usrInput;
myArray.push_back(usrInput);
}
myArray[0][4] = '\0';
cout << myArray[0];
return 0;
}
You can combine the std::string method substr with the standard algorithm std::for_each to apply your cutting function to all strings in the vector.
#include <algorithm> // std::for_each
std::cout << "cut at length: ";
if(size_t cutpoint; std::cin >> cutpoint) {
std::for_each(myArray.begin(), myArray.end(), [&cutpoint](std::string& str) {
str = str.substr(0, cutpoint);
});
}
the std::string class provides a method substr which do the job.
Example:
int main()
{
int maxSize;
cin >> maxSize;
string usrInput;
vector<string> myArray;
for(int i = 0; i < maxSize; i++)
{
cin >> usrInput;
myArray.push_back(usrInput);
}
for ( auto& s: myArray )
{
s = s.substr(0,4);
std::cout << s << std::endl;
}
return 0;
}
http://www.cplusplus.com/reference/string/string/substr/
or
https://en.cppreference.com/w/cpp/string/basic_string/substr
Or you may want use resize which let the resulting string to be at a fixed size, if needed, filled up by a char which you can add as parameter.
https://en.cppreference.com/w/cpp/string/basic_string/resize
What you did is simply replacing a character inside the string. So if you have a string "abcdef" you will get "abcd\0e" which is not what you expect. You can see that yourself by printing out each of the chars like this:
myArray[0][4]='\0';
for ( auto&c: myArray[0] )
{
std::cout << (int) c << std::endl;
}
If you print it as a c-string, the output looks like the string is shorted, but it is really not! This one looks well but is wrong:
myArray[0][4]='\0';
std::cout << myArray[0].c_str() << std::endl;
Why?:
Quite simple: std::cout uses for printing std::string a different method than for printing c-style strings.
If you know where you want to cut, this is why substr function exist. Its a method from string, look at the documentation http://www.cplusplus.com/reference/string/string/substr/.
Hope it helps.

pass string to bool function

How can I get tis to work by passing a string to a Boolean function? I need to have the user input a series of strings, and after each entry, the program should give feedback depending upon whether or not the string fit the given criteria. The string should contain the substring of "1101", without any letters. Thanks for all your help
#include <iostream>
#include <cstring> // for strstr
#include <string>
#include <cctype>
using namespace std;
bool stringCompare(char*y);
string str2;
int main ()
{
string str1, str2;
str1= "1101";
do
{
cout << "Please enter your string: " << endl;
cin >> str2;
while((stringCompare(str2)) == true)
{
if(strstr(str2.c_str(),str1.c_str())) // Primary string search function
{
cout << "ACCEPTED " << endl;
}
else
cout << "NOT ACCEPTED " << endl;
}
} while (2 > 1);
return 0;
}
bool stringCompare(char*y)
{
for(int a = 0; a < strlen(str2); a++)
{
if (!isdigit(str2[a]))
return false;
}
return true;
}
stringCompare takes a parameter of type char*, but you're trying to pass a std::string. That won't work.
You can either use the c_str method of std::string, to get a const char* pointing to the std::string's internal char array. This means that you'll have to chance the parameter to const char*.
Or, much better, you can replace stringCompare to take a reference to a std::string:
bool stringCompare(string& y)
and change strlen(str2) to str2.length(). (Or even better, replace the whole loop to simply:
for(char& ch : str2) // range-based for-loop loops over the entire str2
{
if (!isdigit(ch))
return false;
}
)
Also, you don't need to compare the return value == true. Just do:
while(stringCompare(str2))