Conversion of string to string * - c++

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.
}

Related

reverse string program is not working correctly

I tried writing a string reversing program in C++. Though it seemed really simple, Idk why I ain't getting the correct output.
Here's my code:-
#include<iostream>
#include<string>
using namespace std;
string Reverse_string( string &s ){
for( size_t i{}; i<s.size() ; i++ ){
s.at(i)= s.at( s.size()-1-i );
}
return s;
}
int main(){
cout<<"Enter the string you want to reverse: ";
string s{};
getline(cin,s);
cout<<"\nThe reversed string is :"<< Reverse_string(s) << endl;
return 0;
}
Here is my output:-
Enter the string you want to reverse: string
The reversed string is :gniing
Please help me finding the bug!
In your code you are assigning the values s.at(i)= s.at( s.size()-1-i ); and thus it is changing the previous value of s.at(i), but you should swap the values of respective indexes, s.at(i) with s.at(s.size()-1-i). And also you only need to traverse the first half of the string to swap with respective last half of the string.
This works fine.
#include<iostream>
#include<string>
using namespace std;
string Reverse_string( string &s ){
for( size_t i{}; i<s.size()/2 ; i++ ){ // here you only need to go half
swap(s.at(i), s.at( s.size()-1-i )); // swap is builtin function
}
return s;
}
int main(){
cout<<"Enter the string you want to reverse: ";
string s{};
getline(cin,s);
cout<<"\nThe reversed string is :"<< Reverse_string(s) << endl;
return 0;
}
Instead of using functions. You can just use the headerfile #include <algorithm>. There is a built in reverse function.
#include <iostream>
#include <algorithm>
int main()
{
std::string s;
std::cout << "Enter the string you want to reverse: ";
std::cin >> s;
std::reverse(s.begin(), s.end());
std::cout << "The reversed string is: " << s;
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;
}

can't see the value in main function that I passed to another function in c++

I am trying to copy the content of a string from one variable to another. I don't get any error but nothing gets printed. Any help would be really appreciated.
Following is the code:
#include <iostream>
using namespace std;
string strcopy(string &s1, string &s2)
{
int i=0;
for(i=0;s1[i]!='\0';i++)
s2[i]=s1[i];
s2[i]='\0';
return s2;
}
int main()
{
string str1,str2;
cout<<"Enter a string:";
getline(cin,str1);
str2=strcopy(str1, str2);
cout<<str2;
return 0;
}
You are accessing s2 (which is a reference to str2) out of bounds. The std::string::operator[] doesn't do any bounds check and it definitely doesn't increase the size of the string.
You seem to be doing a C-ish kind of string copying. In C++ that's not necessary. std::string has an operator=. Use it:
int main()
{
std::string str1,str2;
std::cout << "Enter a string:";
std::getline(cin,str1);
str2 = str1; // bam, so simple
std::cout<<str2;
return 0;
}
Another error in your code is that you don't include <string>. And I suggest to avoid using namespace std.
#include <iostream>
int main()
{
string str1,str2;
cout<<"Enter a string:";
getline(cin,str1);
stusing namespace std;
string strcopy(string &s1, string &s2)
{
//int i=0;
//for(i=0;i<s1.length();i++)
//s2[i]=s1[i];
//s2[i]='\0';
s2=s1;
return s2;
}
int main()
{
string str1,str2;
cout<<"Enter a string:";
getline(cin,str1);
str2=strcopy(str1, str2);
cout<<str2;
return 0;
}

Invalid use of void expression in strcmp

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);

How to Convert a C++ String to Uppercase

I need to convert a string in C++ to full upper case. I've been searching for a while and found one way to do it:
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string input;
cin >> input;
transform(input.begin(), input.end(), input.begin(), toupper);
cout << input;
return 0;
}
Unfortunately this did not work and I received this error message:
no matching function for call to 'transform(std::basic_string::iterator, std::basic_string::iterator, std::basic_string::iterator,
I've tried other methods that also did not work. This was the closest to working.
So what I'm asking is what I am doing wrong. Maybe my syntax is bad or I need to include something. I am not sure.
I got most of my info here:
http://www.cplusplus.com/forum/beginner/75634/
(last two posts)
You need to put a double colon before toupper:
transform(input.begin(), input.end(), input.begin(), ::toupper);
Explanation:
There are two different toupper functions:
toupper in the global namespace (accessed with ::toupper), which comes from C.
toupper in the std namespace (accessed with std::toupper) which has multiple overloads and thus cannot be simply referenced with a name only. You have to explicitly cast it to a specific function signature in order to be referenced, but the code for getting a function pointer looks ugly: static_cast<int (*)(int)>(&std::toupper)
Since you're using namespace std, when writing toupper, 2. hides 1. and is thus chosen, according to name resolution rules.
Boost string algorithms:
#include <boost/algorithm/string.hpp>
#include <string>
std::string str = "Hello World";
boost::to_upper(str);
std::string newstr = boost::to_upper_copy("Hello World");
Convert a String In C++ To Upper Case
Try this small program, straight from C++ reference
#include <iostream>
#include <algorithm>
#include <string>
#include <functional>
#include <cctype>
using namespace std;
int main()
{
string s;
cin >> s;
std::transform(s.begin(), s.end(), s.begin(), std::ptr_fun<int, int>(std::toupper));
cout << s;
return 0;
}
Live demo
You could do:
string name = "john doe"; //or just get string from user...
for(int i = 0; i < name.size(); i++) {
name.at(i) = toupper(name.at(i));
}
Uppercase to Lowercase and viceversa using BitWise operators
1.
string s = "cAPsLock";
for(char &c: s)
c = c | ' '; // similar to: c = tolower(c);
cout << s << endl; // output: capslock
string s = "cAPsLock";
for(char &c: s)
c = c & ~' '; // similar to: c = toupper(c);
cout << s << endl; // output: CAPSLOCK
PS: for more info check this link
#include <iostream>
using namespace std;
//function for converting string to upper
string stringToUpper(string oString){
for(int i = 0; i < oString.length(); i++){
oString[i] = toupper(oString[i]);
}
return oString;
}
int main()
{
//use the function to convert string. No additional variables needed.
cout << stringToUpper("Hello world!") << endl;
return 0;
}
Like leemes said, you can use toupper(int). Like this:
void ToUpper(string &str) {
for (auto beg = str.begin(); beg != str.end(); ++beg) {
*beg = toupper(*beg);
}
}
It'll through in each character from str and convert it to upper. Example:
int main()
{
string name;
cout << "Insert a name: ";
cin >> name;
ToUpper(name);
cout << "Name in upper case: " << name << endl;
}
You can also use the function from code below to convert it to Upper-case.
#include<iostream>
#include<cstring>
using namespace std;
//Function for Converting Lower-Case to Upper-Case
void fnConvertUpper(char str[], char* des)
{
int i;
char c[1 + 1];
memset(des, 0, sizeof(des)); //memset the variable before using it.
for (i = 0; i <= strlen(str); i++) {
memset(c, 0, sizeof(c));
if (str[i] >= 97 && str[i] <= 122) {
c[0] = str[i] - 32; // here we are storing the converted value into 'c' variable, hence we are memseting it inside the for loop, so before storing a new value we are clearing the old value in 'c'.
} else {
c[0] = str[i];
}
strncat(des, &c[0], 1);
}
}
int main()
{
char str[20]; //Source Variable
char des[20]; //Destination Variable
//memset the variables before using it so as to clear any values which it contains,it can also be a junk value.
memset(str, 0, sizeof(str));
memset(des, 0, sizeof(des));
cout << "Enter the String (Enter First Name) : ";
cin >> str; //getting the value from the user and storing it into Source variable.
fnConvertUpper(str, des); //Now passing the source variable(which has Lower-Case value) along with destination variable, once the function is successfully executed the destination variable will contain the value in Upper-Case
cout << "\nThe String in Uppercase = " << des << "\n"; //now print the destination variable to check the Converted Value.
}