Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 29 days ago.
Improve this question
I want to write program that can get string as argument. But string like "Hell o w o r l d" will loose all the spaces if i am trying to get it from argv. Is there a way to get number of spaces between each two of arguments, or better just get argv as string?
One way to preserve the spaces in the original string is to put the string in quotes when passing it as an argument. For example, if you want to pass the string "Hello world" as an argument, you would run the program like this:
./file "Hello World"
Then, in your program, you can access the original string, including the spaces, by using argv[1].
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Suppose I have this string
str = "The rain in Spain"
And I want to return the first word that contains the two letters "ai" (i.e. "rain")
What must the regular expression be?
Following Regex can be used to find the first word in any given string
\w*ai\w*/m
/m param is for multi line & will return the first match
replace ai within the parenthesis to the required char matching for any string. i.e ab | we | slkd etc
Hopefully this will help you.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to make a function that takes the first and last letters of a string, divides them for the modulus and stores them in an array spot related to the modulus number. However I don't know how I would get the first and last letters of the string.
Assuming std::string str, you can use either:
str.front() and str.back() (C++11 and later only).
str[0] and str[str.size()-1], if str is not empty.
You can have the first letter of your string by just using yourString[0] and to get the last latter you can use yourString[yourString.size()-1]
EDIT
As said in the comments, assuming you're using std::string
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am writting a code in C++. And I have a string like this
std::string1 = "one
two
end"
this string has got a 2 newline. I want to save this string in the one line of file. for example:
std::string2 = "one\ntwo\nend"
End after I read from file this line and I change again string2 like a string1.
Do you know how I can do this??
My problem is to call the function to create a random strings and these strings maybe have got a newlines but I want to save and read from file the only one line. And this reason I don't know the string because it is random.
You can use raw string literals for this:
std::string1 myString = R"__(one
two
three)__";
The two underscores __ work as delimiters which can be chosen freely. Use the same in the beginning and the end of the raw string literal.
Also notice that anything in between will become part of the string, including all whitespace characters.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Replace the two dots by code, which will return "over" from string yay to string variable over.
string yay = “semester almost over”;
string over = ..
Confused by the question, the previous q's used .find() and it was simple, this one isn't even compiling.
Not sure what it is that is not compiling for you, but this is what you want:
string over = yay.substr(yay.find("over"),4);
To break it down: yay.find("over") returns index of the first letter of "over" in 'yay', which is 16. yay.substr(16,4) extracts 4 characters from 'yay' starting at index 16.
string over = yay.find("over");
Does not work because yay.find does not return a string, it returns an index
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i'm trying to understand this library that stanford uses for their CS course but i can't just use this feature in my CS course back in my home. How do i emulate this feature? i was wondering is this a getline(cin, integer)? or is this something to do with fstream? The reason being, i'm trying to go through the lecture series at UDEMY.
Here is the definition.
int getInteger(string prompt = "");
Reads a complete line from cin and scans it as an integer. If the scan
succeeds, the integer value is returned. If the argument is not a
legal integer or if extraneous characters (other than whitespace)
appear in the string, the user is given a chance to reenter the value.
If supplied, the optional prompt string is printed before reading the
value.
Usage:
int n = getInteger(prompt);
You can download simpio.h here: http://www.eecs.wsu.edu/~cs150/prog/libs.htm
If you can't or don't want to use the library, you could study its source code.