I have a bit of a confusingly worded question


Flag Post

ZCuttill

asked
3

I have a bit of a confusingly worded question

2 Answers


[{"insert":"Nevermind, I figured it out! This is how I ended up doing what I wanted to do:\n\nbool spaceFound = false;\n  int step = 0;\n  while((!spaceFound) && (step < totalWords))\n  {\n   if(str[step] != ' ')\n    step++;\n   else\n    spaceFound = true;\n  }\n  string s = str.substr(i, step+1);\n"}]

[{"insert":"Hi R0NIN707,\nC++ actually has a built-in function called \"strtok\" that is perfect for this scenario. The function returns the first word separated by the specified character (in this case the space). You can place it in a while loop to search through a string containing multiple words separated by spaces (or any character) and then store the result in an array. Here's an implementation of the function:\n#include <stdio.h> "},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"#include <string.h> "},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"  "},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"int main() "},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"{ "},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"    char str[] = \"Code Assist Collaboration is awesome\";"},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"  "},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"    // Returns first token  "},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"    char *token = strtok(str, \" \");"},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"    "},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"    // Keep printing tokens until"},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"    // string doesn't contain spaces"},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"    while (token != NULL) "},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"    { "},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"        printf(\"%s\\n\", token);"},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"        // save the token in an array here"},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"        // do other opperations"},{"attributes":{"code-block":true},"insert":"\n\n"},{"insert":"        token = strtok(NULL, \" \"); "},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"    } "},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"  "},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"    return 0; "},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"}"},{"attributes":{"code-block":true},"insert":"\n"},{"insert":"strtok takes 2 arguments:\nString to be searched"},{"attributes":{"list":"ordered"},"insert":"\n"},{"insert":"Delimiter (fancy word for the word separator)"},{"attributes":{"list":"ordered"},"insert":"\n"},{"insert":"Hope that helps!\nI actually created and shared a project (called \"C++ split string\") with you using the "},{"attributes":{"bold":true},"insert":"Code Assist Projects tool "},{"insert":"so that you can play around with the code a little bit. The link to the project is: https://codeassist.org/projects/5b9dc070c37f7724b2b510c0/.\n"}]

Your Answer


Feedback