I'm taking in string inputs and I need to read in multiple words from one line and push them into a vector. I can't for the life of me figure out how to split the string whenever there's a space character. I know I need to use the getline() function, but I feel like I've tried everything and I'm stuck. Here's my code so far. I'm aware that the if/else statement are both doing the same thing. int main(){     vector&lt;string&gt; vec;     string str = &quot;&quot;;     string t = &quot;&quot;;     int i = 0;      while(getline(cin, str){         if(str != &quot;STOP&quot;){             if(str[i] == ' '){                 vec.push_back(str);             }else{                 vec.push_back(str);             }         }else{             cout &lt;&lt; vec.size() &lt;&lt; endl;         }         i += 1;     } }  I can't really use any fancy code, just the basic functions
2094
I'm trying to iterate over the words of a string.  The string can be assumed to be composed of words separated by whitespace.  Note that I'm not interested in C string functions or that kind of character manipulation/access. Also, please give precedence to elegance over efficiency in your answer.  The best solution I have right now is:  #include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;string&gt;  using namespace std;  int main() {     string s = "Somewhere down the road";     istringstream iss(s);      do     {         string subs;         iss &gt;&gt; subs;         cout &lt;&lt; "Substring: " &lt;&lt; subs &lt;&lt; endl;     } while (iss); }   Is there a more elegant way to do this?
37813
I want to split 'hello' into h e l l o in an array using only bash, I could do it in sed with sed 's/./&amp; /g' but I want to know how to split a string into an array in Bash when I do not know what the delimiter would be, or the delimiter is any single character.  I don't think I can use ${i// /} without some creativity because the delimiter is an unknown, and I don't think that expression accepts regex.  I tried using BASH_REMATCH with [[ string =~ ([a-z].).* ]] but it doesn't work as I expected.  What is the proper way to use only bash to accomplish a string.split() type of behavior?  The reason is that I am trying to write the rev utility in all bash:    while read data; do   word=($(echo $data|tr ' ' '_'|sed 's/./&amp; /g'))   new=()   i=$((${#word[@]} - 1))   while [[ $i -ge 0 ]]; do     new+=(${word[$i]})     (( i-- ))   done   echo ${new[@]}|tr -d ' '|tr '_' ' '   done   But I used tr and sed, I want to know how to do the split properly and then I will fix it to be all bash.  Just for fun.
6767
Is there a character in JavaScript to break up a line of code so that it is read as continuous despite being on a new line?  Something like....   1. alert ( "Please Select file    2. \ to delete" );
4322
I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?  For example, adding a bunch of strings,  e = 'a' + 'b' + 'c' + 'd'   and have it in two lines like this:  e = 'a' + 'b' +     'c' + 'd'
7406
Java has a convenient split method:  String str = "The quick brown fox"; String[] results = str.split(" ");   Is there an easy way to do this in C++?
100697
char *strtok(char *s1, const char *s2)      repeated calls to this function break string s1 into "tokens"--that is   the string is broken into substrings,   each terminating with a '\0', where   the '\0' replaces any characters   contained in string s2. The first call   uses the string to be tokenized as s1;   subsequent calls use NULL as the first   argument. A pointer to the beginning   of the current token is returned; NULL   is returned if there are no more   tokens.   Hi,   I have been trying to use strtok just now and found out that if I pass in a char* into s1, I get a segmentation fault. If I pass in a char[], strtok works fine.   Why is this?  I googled around and the reason seems to be something about how char* is read only and char[] is writeable. A more thorough explanation would be much appreciated.
71970
I arrive to output a string on multiple lines inside a retangle but haven't find a way to reduce or enlarge the line spacing. How to do that?
7366
How do I read a file into a std::string, i.e., read the whole file at once?  Text or binary mode should be specified by the caller. The solution should be standard-compliant, portable and efficient. It should not needlessly copy the string's data, and it should avoid reallocations of memory while reading the string.  One way to do this would be to stat the filesize, resize the std::string and fread() into the std::string's const_cast&lt;char*&gt;()'ed data(). This requires the std::string's data to be contiguous which is not required by the standard, but it appears to be the case for all known implementations. What is worse, if the file is read in text mode, the std::string's size may not equal the file's size.  A fully correct, standard-compliant and portable solutions could be constructed using std::ifstream's rdbuf() into a std::ostringstream and from there into a std::string. However, this could copy the string data and/or needlessly reallocate memory.    Are all relevant standard library implementations smart enough to avoid all unnecessary overhead?  Is there another way to do it?  Did I miss some hidden Boost function that already provides the desired functionality?     void slurp(std::string&amp; data, bool is_binary)
50273
Is there a utility that split file by newline symbol? e.g if a file contains the following lines,  aa bbb cccc   If I want to split it to 3 files, the desired output would be:  aa, bbb And cccc (in 3 different files)  I already checked the split command, it only cut file by file sizes, not what I want.  If I don't wrote a utility myself, is there any standard tool to use?
51028
I'm splitting a string by three different characters but I want the output to include the characters I split by. Is there any easy way to do this?
34668
\documentclass{article} \usepackage{amsmath} \begin{document} \begin{equation*}   X_i =    \begin{cases}     \left[\begin{smallmatrix}       0 \\ 1 \\ 2     \end{smallmatrix}\right]     &amp;     \text{if $i$ is even;}     \\     \left[\begin{smallmatrix}       2 \\ 1 \\ 0     \end{smallmatrix}\right]     &amp;     \text{otherwise.}   \end{cases} \end{equation*} \end{document}   This is the LaTeX code that I wrote based on a cases environment. There's no space between the matrices on the right. What's the best way to add some vertical spaces to separate them?    p.s. This is a simplified form of my original writing, so I cannot just accept suggestions like 'use a matrix transpose to transform the column vector into a row vector.'
6296
What regex pattern would need I to pass to  java.lang.String.split()  to split a String into an Array of substrings using all whitespace characters (' ', '\t', '\n', etc.) as delimiters?
690
What is the difference between \par, \newline, \\ and blank lines? When should I use which one?
97702
I'm trying to split a string with all non-alphanumeric characters as delimiters yet Java's String.split() method discards the delimiter characters from the resulting array. Is there a way to split a string like the "\W" regex pattern does, yet keep the delimiters?
22788
How do you reverse a string in C or C++ without requiring a separate buffer to hold the reversed string?
11109
I've been having a lot of problems trying to figure out how to use scanf().  It seems to work fine with integers, being fairly straight forward scanf("%d", &amp;i).  Where I am running into issues is using scanf() in loops trying to read input.  For example:  do {   printf("counter: %d: ", counter);   scanf("%c %c%d", &amp;command, &amp;prefix, &amp;input); } while (command != 'q');    When I enter in a validly structured input like c P101, it seems to loop again before prompting me.  This seems to happen even with a single:  scanf("%c", &amp;c)    in a while loop.  It'll do the loop twice before prompting me again.  What is making it loop twice, and how do I stop it? When I enter in less amount of input that programmatically wouldn't have another character or number such as q, pressing enter seems to prompt me to enter more.  How do I get scanf() to process both single and double character entries?
63970
Possible Duplicate:          I want to write a function that reverses the given string passed into it. But, I can not. If I supply the doReverse function (see code below) with a character array, my code works well.  I can't figure out why this does not work. I am able to access str[0] in doReverse, but I can't change any value of the array by using a char pointer. Any ideas?  void doReverse(char *str) {     str[0] = 'b'; }  void main(void) {     char *str = "abc";     doReverse(str);     puts(str); }   Update:  I know how to do write a reverse function by passing a character array to it:  void reverse1(char p[]) {     int i, temp, y;      for (i = 0, y = strlen(p); i &lt; y; ++i, --y) {         temp = p[y-1];         p[y-1] = p[i];         p[i] = temp;     } }   But, I want to write another version that gets a char pointer as a parameter.