I think you're thinking beyond his scope. If he's using strings it's probably for something simple, like storing a word into a variable with cin and printing the same thing out later with cout. I don't think you can do more than that without treating them like arrays of characters, which he obviously isn't doing if he's not doing arrays.
When you declare a variable in C++, you must specify the data type.
Example:
int a = 0.
a is an integer type int and is assigned the value of 0.
Strings are the same way.
You have either
std::string str = "Hello World";
char[] str = "Hello World";
char* str = "Hello World";
const char* str = "Hello World";
He's using namespace std, meaning he'd just declare the variable as a string. My point is that he doesn't have any decent tools at his disposal at this point in time to solve this problem using strings.
Okay.
That kind of brings me back to the original std thing.
std is the namespace of the standard. You use this (std::<object>) to specify that the object comes from the standard library. When you use "using namespace std;" you're dragging the entire std namespace into your code so you wouldn't write std::<object>, rather you'd write <object> instead. This is looked down upon and I hope that they eventually teach you to stop, but for now I guess it doesn't matter.
Except he has a problem regarding it actually.
Originally Posted by Imagine
I'm not familiar with how cstring works, but if you can't compare one string to another string using an if statement, I don't think he or I could solve that problem. Regardless, it seems like the focus of the problem is to use a loop to determine if two things are the same or not, not fight with a bunch of syntax with strings that you can't properly do without arrays or pointers.
Bumping cause I have another midterm.
Options for ??? WHAT GOES HERE ??? are:Code:string reverse(string s) { char temp; int i = 0; int j = s.size() – 1; while (i < j) { ??? WHAT GOES HERE ??? } return s; }
The purpose of the function is to to flip around a string (i.e testing turns in gnitset).Code:A.temp = s.substr(0, i) s = s.substr(0,j) + temp; B. temp = s.at(i); s.at(i) = s.at(j); s.at(j) = temp; C.temp = s.substr(j); s = temp + s.substr(i, j); D.s.at(i) = s.at(j); s.at(j) = s.at(i); E.temp = s.at(i); s = s.substr(i + 1); s += temp; 6.s = s.substr(j) + s.substr(i);
I would assume i is incremented in this problem and that j is decremented. With that assumption, would the correct answer be B?
My logic for B is that we have a temp variable to hold of s.at(i) so that it won't be lost, and that once s.at(i) is set to s.at(j), s.at(j) is given the char stored in s.at(i) through temp. Then the loop would keep going until i = j (odd # of letters) or i = j + 1 (even # of letters).
None of the above, actually, since none of them move i and j.
But if we ignore that, then yes, it's B.
There is a faster way to do this.
But if you want to do it manually..Code:string reverse(string s) { return string(s.rend(), s.rbegin()); }
Code:string reverse(string s) { char temp; int length = s.length() - 1; int numSwaps = s.length() >> 1; for(int i = 0; i < numSwaps; i++) { temp = s[i]; s[i] = s[length-i]; s[length-i] = temp; } return s; }
Please tell me you're writing this yourself because that function coming from an instructor is pretty bad. (No offence, OP)
@Fiel;
Or save yourself the function rewrite and do std::reverse(str.begin(),str.end());
Wow that's your professor's code?
Holy shit that's awful.
|
Bookmarks