This question already has answers here:
How do you reverse a string in place in C or C++?
(21 answers)
Closed 7 years ago.
Hey guys I'm new here and a programming noob so bear with me here.
This is for my C++ class which sadly my teacher is terrible at teaching so many things confuse me so I need some help here.
We have a lab that is called 'Reverse Sentence' and this is what it wants In this lab.
Write the function "ReverseSentence" that takes a string parameter and changes it, by reversing it.
For example:
INPUT: the first test
OUTPUT: tset tsrif eht
The function must not use an extra string, but must reverse the elements of the input string.
#include <iostream>
#include <string>
using namespace std;
void ReverseSentence( string& inputSentence){
/* Body of Function Here */
}
int main(){
string inputSentence;
cout << "Input your sentence: ";
getline(cin, inputSentence);
cout << endl;
ReverseSentence(inputSentence);
cout << "Reversed Sentence:" << endl;
cout << inputSentence << endl;
return 0;
}
Can someone please help me what function is because I'm having trouble with it.
Just use std::reverse:
void ReverseSentence( string& inputSentence){
std::reverse(inputSentence.begin(), inputSentence.end());
}
Half of the cycle and swap.
#include<algorithm>
#include<string>
void ReverseSentence(std::string &s){
for (int i = 0; i < s.size()/2; ++i)
std::swap(s[i], s[s.size() - i - 1]);
}
Related
This question already has answers here:
std::cout not printing the value with out endl (newline specifier)
(1 answer)
std::cout won't print
(4 answers)
Closed 3 years ago.
I was just watching a video on youtube about an akm function and i tried to implement it.
I actually wrote a code and forgot to make spaces between the variables(for an easy reading), but the program didn't print anything but it kept on calculating.
I thought that a similar syntax would work just fine. Is there something I am doing wrong ?
Here is the code :
#include <bits/stdc++.h>
using namespace std;
int akm(int m,int n) {
if(m==0) return n+1;
else if(n==0) return akm(m-1,1);
else return akm(m-1 , akm(m,n-1));
}
int main() {
for(int i=0;i<6;i++)
for(int j=0;j<6;j++) {
cout<<i<<" "<<j ;
cout<<akm(i,j);
}
}
You may need to explicitly flush the output stream or print a newline which may flush the buffer on some streams:
std::cout << std::flush;
std::cout << std::endl;
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.
This question already has answers here:
Size of array object passed to function
(4 answers)
Closed 7 years ago.
I'm a C++ beginner, so talk to me like I'm 5.
Here's what I'm trying to do:
Take user's input into a string userInput
Pass userInput, along with 2 arrays (answers and outcomes), into a function answerCheck
Compare userInput with answers array
If there's a match, output string from outcomes
If no match, loop, ask for userInput
I output the size of answers with answersSize. It outputs 1 instead of the expected 2.
I can't figure out how to pass the information in the arrays to the answerCheck function.
Any suggestions?
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int question1();
bool answerCheck(string[], string[], string);
int main() {
question1();
system("pause");
return 0;
}
int question1() {
cout << "Do you want to go LEFT or RIGHT?" << endl;
string answers[2] = { "left", "right" };
string outcomes[2] = { "you went left", "you went right" };
string userInput = "";
getline(cin, userInput);
// outputs correct size of answers array for testing ======
int answersSize = sizeof(answers) / sizeof(string);
cout << "Correct size of answers: "<< answersSize << endl;
// ========================================================
answerCheck(answers, outcomes, userInput);
return 0;
}
bool answerCheck(string answers[], string outcomes[], string userInput){
int answersSize = sizeof(answers) / sizeof(string);
cout << "Size of answers: "<< answersSize << endl;
for(int i=0; i < answersSize; i++){
if(userInput.find(answers[i]) != string::npos){
cout <<"\n" << outcomes[i] <<"\n" << endl;
return true;
}
}
cout << "Try putting in something else." << endl;
return false;
}
The problem is here:
int answersSize = sizeof(answers) / sizeof(string);
If you print it out, you will find that sizeof(answers) is the size of a pointer (4 or 8 bytes), not the size of the entire array. You need to pass the array size in as a function argument, or else use a class type like std::vector which encapsulates this in a more C++ way.
A general advice for beginners would be to use the C++ such as classes std::vector instead of plain C arrays. So in your example, instead of
string answers[2] = { "left", "right" };
use
std::vector<std::string> answers{ "left", "right" };
and declare your function
bool answerCheck(std::vector<string> const& answers,
std::vector<string> const&outcomes,
string const& userInput)
If you are reading a introductory book and it starts by introducing C-style code first, I would throw it away. A good introduction to C++ is e.g. https://isocpp.org/tour.
This question already has answers here:
execute C++ from String variable
(6 answers)
Closed 7 years ago.
Hi I have been trying to make a program that will ask for the users input and whatever the user types it will execute it as a command. Sort of like CMD
#include <iostream>
#include <string>
using namespace std;
string A;
int main(){
for (int i = 0; i > -1; ++i){
cout << "Command: ";
cin >> A;
// Here Would Be The Code
cout << "Command Executed!";
}
}
Here is what i imagine a possible output (if it worked)
Command: cout << "Test";
Test
Command Executed!
You can use the system function.
http://www.cplusplus.com/reference/cstdlib/system/
system("command")
This question already has answers here:
std::cin input with spaces?
(8 answers)
Closed 8 years ago.
The code is not giving desired output
when I type in a string example "Ben Parker", the output is "Goodmorning, Ben" and not the entire name("Ben Parker") what seems to be the problem?
#include <iostream>
#include <stdlib.h>
#include <cstring>
int main() {
char your_name[20];
std::cout << "Enter your name: ";
std::cin >> your_name;
std::cout << "Goodmorning, ";
std::cout.write (your_name, strlen(your_name)) << std::endl;
return 0;
}
SOLUTION
This was a very old question when I just began programming.
The entire character array can be read and printed with a for loop, or better a string type variable can be used, since it is C++.
using string your_name; seems to fix the problem, which can be then printed with a simple std::cout << your_name << endl;
You probably put a space in between "Ben" and "Parker" in input. This would cause the cin logic to believe it had an answer after seeing the space following "Ben". You will probably want to read an entire line at a time to get past that problem. See this page for an example.