How to create a 2D-Array of chars given a string?









up vote
1
down vote

favorite












Given this string (or any string): "# ####n# #n# ## #n# #n### ##n",
how can I I'm working on a shortest path finding maze solver for my data structures and algorithms class. Given this string (or any string), how can I make a 2D array from it? That way, I can just loop through the 2d array and if its a whitespace, I can make a new vertex.



For example, this



"# ####n# #n# ## #n# #n### ##n"



should be stored in the 2D array like so:



 # ####

# #

# ## #

# #

### ##


I tried implementing this but it didn't print out the right thing.



char ** grid;
for(int i = 0; i < maze.size(); i++)
grid[i] = new char[numCols];
for(int j = 0; j != 'n'; j++)
cin >> grid[i][j];











share|improve this question





















  • should be stored in the 2D array like so: This is not an array. How many elements does the second row have?
    – Ayxan
    Nov 10 at 16:46











  • The second row (and all rows) would have 6 items in this case. There would bet two '#' and four whitespaces.
    – Josh Garza
    Nov 10 at 16:49










  • And how come #_# (1 space) is translated as #____# (4 spaces)and not something like #_# (1+3 spaces)?
    – Ayxan
    Nov 10 at 16:51















up vote
1
down vote

favorite












Given this string (or any string): "# ####n# #n# ## #n# #n### ##n",
how can I I'm working on a shortest path finding maze solver for my data structures and algorithms class. Given this string (or any string), how can I make a 2D array from it? That way, I can just loop through the 2d array and if its a whitespace, I can make a new vertex.



For example, this



"# ####n# #n# ## #n# #n### ##n"



should be stored in the 2D array like so:



 # ####

# #

# ## #

# #

### ##


I tried implementing this but it didn't print out the right thing.



char ** grid;
for(int i = 0; i < maze.size(); i++)
grid[i] = new char[numCols];
for(int j = 0; j != 'n'; j++)
cin >> grid[i][j];











share|improve this question





















  • should be stored in the 2D array like so: This is not an array. How many elements does the second row have?
    – Ayxan
    Nov 10 at 16:46











  • The second row (and all rows) would have 6 items in this case. There would bet two '#' and four whitespaces.
    – Josh Garza
    Nov 10 at 16:49










  • And how come #_# (1 space) is translated as #____# (4 spaces)and not something like #_# (1+3 spaces)?
    – Ayxan
    Nov 10 at 16:51













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Given this string (or any string): "# ####n# #n# ## #n# #n### ##n",
how can I I'm working on a shortest path finding maze solver for my data structures and algorithms class. Given this string (or any string), how can I make a 2D array from it? That way, I can just loop through the 2d array and if its a whitespace, I can make a new vertex.



For example, this



"# ####n# #n# ## #n# #n### ##n"



should be stored in the 2D array like so:



 # ####

# #

# ## #

# #

### ##


I tried implementing this but it didn't print out the right thing.



char ** grid;
for(int i = 0; i < maze.size(); i++)
grid[i] = new char[numCols];
for(int j = 0; j != 'n'; j++)
cin >> grid[i][j];











share|improve this question













Given this string (or any string): "# ####n# #n# ## #n# #n### ##n",
how can I I'm working on a shortest path finding maze solver for my data structures and algorithms class. Given this string (or any string), how can I make a 2D array from it? That way, I can just loop through the 2d array and if its a whitespace, I can make a new vertex.



For example, this



"# ####n# #n# ## #n# #n### ##n"



should be stored in the 2D array like so:



 # ####

# #

# ## #

# #

### ##


I tried implementing this but it didn't print out the right thing.



char ** grid;
for(int i = 0; i < maze.size(); i++)
grid[i] = new char[numCols];
for(int j = 0; j != 'n'; j++)
cin >> grid[i][j];








c++ multidimensional-array graph maze






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 16:26









Josh Garza

337




337











  • should be stored in the 2D array like so: This is not an array. How many elements does the second row have?
    – Ayxan
    Nov 10 at 16:46











  • The second row (and all rows) would have 6 items in this case. There would bet two '#' and four whitespaces.
    – Josh Garza
    Nov 10 at 16:49










  • And how come #_# (1 space) is translated as #____# (4 spaces)and not something like #_# (1+3 spaces)?
    – Ayxan
    Nov 10 at 16:51

















  • should be stored in the 2D array like so: This is not an array. How many elements does the second row have?
    – Ayxan
    Nov 10 at 16:46











  • The second row (and all rows) would have 6 items in this case. There would bet two '#' and four whitespaces.
    – Josh Garza
    Nov 10 at 16:49










  • And how come #_# (1 space) is translated as #____# (4 spaces)and not something like #_# (1+3 spaces)?
    – Ayxan
    Nov 10 at 16:51
















should be stored in the 2D array like so: This is not an array. How many elements does the second row have?
– Ayxan
Nov 10 at 16:46





should be stored in the 2D array like so: This is not an array. How many elements does the second row have?
– Ayxan
Nov 10 at 16:46













The second row (and all rows) would have 6 items in this case. There would bet two '#' and four whitespaces.
– Josh Garza
Nov 10 at 16:49




The second row (and all rows) would have 6 items in this case. There would bet two '#' and four whitespaces.
– Josh Garza
Nov 10 at 16:49












And how come #_# (1 space) is translated as #____# (4 spaces)and not something like #_# (1+3 spaces)?
– Ayxan
Nov 10 at 16:51





And how come #_# (1 space) is translated as #____# (4 spaces)and not something like #_# (1+3 spaces)?
– Ayxan
Nov 10 at 16:51













1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










Not sure that you deal with a fixed number of lines. The example below is conservative, it assumes that you do not know in advance the number of lines and that each line could have different length, hence it uses a vector of vector of string.



You can replace this with arrays if you know the size in advance. Comments should make the behavior clear.



#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;

int main()

const char *s = "# ####n# #n# ## #n# #n### ##n";

istringstream is(s); // associate the input string with an input stream

vector<string> result; // use string for each line because a vector of characters is a string

string line;
while(getline(is, line)) // use stream library to read until next n character
result.push_back(line); // add line

// demonstrate results
for (size_t i = 0; i < result.size(); ++i)
for (size_t j = 0; j < result[i].length(); ++j)
cout << result[i][j];
cout << "n";


return 0;






share|improve this answer






















  • Wow, this is very helpful. I ran it on a online compiler very quickly and everything printed out correctly except the 2nd and 4th row. The second hashtag is in the 3rd column when it should be in the last column. Also, why did you use const char *s to store the string?
    – Josh Garza
    Nov 10 at 16:55










  • If you want it in the last column, then you need to add more spaces. The second line instance of being "#.#n" should be "# ....#n, unless there is a grammar rules in these things which you have not specified. Note that I replaced spaces with dots in this message, otherwise the message does not render correctly. const char * is just the type for C-style raw strings.
    – Fabio
    Nov 10 at 17:00











  • Ohhh I see. I just realized that the I didn't copy the string correctly so the spaces are off. But it works now. Is there a way to access individual elements by s[i][j]?
    – Josh Garza
    Nov 10 at 17:11










  • Also, how could I use the traditional nested for loops instead of the range-based for loops?
    – Josh Garza
    Nov 10 at 17:18










  • Yes, you can use v[i][j]. If you know the size in advance, do not use vector, a C-style array will do, it will simplify the example quite a bit. Yes, you could use normal loops. I see you are new to this web site. If you think this is the correct answer, you may consider up-voting it and marking it as correct.
    – Fabio
    Nov 10 at 17:24











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240967%2fhow-to-create-a-2d-array-of-chars-given-a-string%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










Not sure that you deal with a fixed number of lines. The example below is conservative, it assumes that you do not know in advance the number of lines and that each line could have different length, hence it uses a vector of vector of string.



You can replace this with arrays if you know the size in advance. Comments should make the behavior clear.



#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;

int main()

const char *s = "# ####n# #n# ## #n# #n### ##n";

istringstream is(s); // associate the input string with an input stream

vector<string> result; // use string for each line because a vector of characters is a string

string line;
while(getline(is, line)) // use stream library to read until next n character
result.push_back(line); // add line

// demonstrate results
for (size_t i = 0; i < result.size(); ++i)
for (size_t j = 0; j < result[i].length(); ++j)
cout << result[i][j];
cout << "n";


return 0;






share|improve this answer






















  • Wow, this is very helpful. I ran it on a online compiler very quickly and everything printed out correctly except the 2nd and 4th row. The second hashtag is in the 3rd column when it should be in the last column. Also, why did you use const char *s to store the string?
    – Josh Garza
    Nov 10 at 16:55










  • If you want it in the last column, then you need to add more spaces. The second line instance of being "#.#n" should be "# ....#n, unless there is a grammar rules in these things which you have not specified. Note that I replaced spaces with dots in this message, otherwise the message does not render correctly. const char * is just the type for C-style raw strings.
    – Fabio
    Nov 10 at 17:00











  • Ohhh I see. I just realized that the I didn't copy the string correctly so the spaces are off. But it works now. Is there a way to access individual elements by s[i][j]?
    – Josh Garza
    Nov 10 at 17:11










  • Also, how could I use the traditional nested for loops instead of the range-based for loops?
    – Josh Garza
    Nov 10 at 17:18










  • Yes, you can use v[i][j]. If you know the size in advance, do not use vector, a C-style array will do, it will simplify the example quite a bit. Yes, you could use normal loops. I see you are new to this web site. If you think this is the correct answer, you may consider up-voting it and marking it as correct.
    – Fabio
    Nov 10 at 17:24















up vote
3
down vote



accepted










Not sure that you deal with a fixed number of lines. The example below is conservative, it assumes that you do not know in advance the number of lines and that each line could have different length, hence it uses a vector of vector of string.



You can replace this with arrays if you know the size in advance. Comments should make the behavior clear.



#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;

int main()

const char *s = "# ####n# #n# ## #n# #n### ##n";

istringstream is(s); // associate the input string with an input stream

vector<string> result; // use string for each line because a vector of characters is a string

string line;
while(getline(is, line)) // use stream library to read until next n character
result.push_back(line); // add line

// demonstrate results
for (size_t i = 0; i < result.size(); ++i)
for (size_t j = 0; j < result[i].length(); ++j)
cout << result[i][j];
cout << "n";


return 0;






share|improve this answer






















  • Wow, this is very helpful. I ran it on a online compiler very quickly and everything printed out correctly except the 2nd and 4th row. The second hashtag is in the 3rd column when it should be in the last column. Also, why did you use const char *s to store the string?
    – Josh Garza
    Nov 10 at 16:55










  • If you want it in the last column, then you need to add more spaces. The second line instance of being "#.#n" should be "# ....#n, unless there is a grammar rules in these things which you have not specified. Note that I replaced spaces with dots in this message, otherwise the message does not render correctly. const char * is just the type for C-style raw strings.
    – Fabio
    Nov 10 at 17:00











  • Ohhh I see. I just realized that the I didn't copy the string correctly so the spaces are off. But it works now. Is there a way to access individual elements by s[i][j]?
    – Josh Garza
    Nov 10 at 17:11










  • Also, how could I use the traditional nested for loops instead of the range-based for loops?
    – Josh Garza
    Nov 10 at 17:18










  • Yes, you can use v[i][j]. If you know the size in advance, do not use vector, a C-style array will do, it will simplify the example quite a bit. Yes, you could use normal loops. I see you are new to this web site. If you think this is the correct answer, you may consider up-voting it and marking it as correct.
    – Fabio
    Nov 10 at 17:24













up vote
3
down vote



accepted







up vote
3
down vote



accepted






Not sure that you deal with a fixed number of lines. The example below is conservative, it assumes that you do not know in advance the number of lines and that each line could have different length, hence it uses a vector of vector of string.



You can replace this with arrays if you know the size in advance. Comments should make the behavior clear.



#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;

int main()

const char *s = "# ####n# #n# ## #n# #n### ##n";

istringstream is(s); // associate the input string with an input stream

vector<string> result; // use string for each line because a vector of characters is a string

string line;
while(getline(is, line)) // use stream library to read until next n character
result.push_back(line); // add line

// demonstrate results
for (size_t i = 0; i < result.size(); ++i)
for (size_t j = 0; j < result[i].length(); ++j)
cout << result[i][j];
cout << "n";


return 0;






share|improve this answer














Not sure that you deal with a fixed number of lines. The example below is conservative, it assumes that you do not know in advance the number of lines and that each line could have different length, hence it uses a vector of vector of string.



You can replace this with arrays if you know the size in advance. Comments should make the behavior clear.



#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;

int main()

const char *s = "# ####n# #n# ## #n# #n### ##n";

istringstream is(s); // associate the input string with an input stream

vector<string> result; // use string for each line because a vector of characters is a string

string line;
while(getline(is, line)) // use stream library to read until next n character
result.push_back(line); // add line

// demonstrate results
for (size_t i = 0; i < result.size(); ++i)
for (size_t j = 0; j < result[i].length(); ++j)
cout << result[i][j];
cout << "n";


return 0;







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 6:18









Prix

15.9k1252113




15.9k1252113










answered Nov 10 at 16:43









Fabio

856319




856319











  • Wow, this is very helpful. I ran it on a online compiler very quickly and everything printed out correctly except the 2nd and 4th row. The second hashtag is in the 3rd column when it should be in the last column. Also, why did you use const char *s to store the string?
    – Josh Garza
    Nov 10 at 16:55










  • If you want it in the last column, then you need to add more spaces. The second line instance of being "#.#n" should be "# ....#n, unless there is a grammar rules in these things which you have not specified. Note that I replaced spaces with dots in this message, otherwise the message does not render correctly. const char * is just the type for C-style raw strings.
    – Fabio
    Nov 10 at 17:00











  • Ohhh I see. I just realized that the I didn't copy the string correctly so the spaces are off. But it works now. Is there a way to access individual elements by s[i][j]?
    – Josh Garza
    Nov 10 at 17:11










  • Also, how could I use the traditional nested for loops instead of the range-based for loops?
    – Josh Garza
    Nov 10 at 17:18










  • Yes, you can use v[i][j]. If you know the size in advance, do not use vector, a C-style array will do, it will simplify the example quite a bit. Yes, you could use normal loops. I see you are new to this web site. If you think this is the correct answer, you may consider up-voting it and marking it as correct.
    – Fabio
    Nov 10 at 17:24

















  • Wow, this is very helpful. I ran it on a online compiler very quickly and everything printed out correctly except the 2nd and 4th row. The second hashtag is in the 3rd column when it should be in the last column. Also, why did you use const char *s to store the string?
    – Josh Garza
    Nov 10 at 16:55










  • If you want it in the last column, then you need to add more spaces. The second line instance of being "#.#n" should be "# ....#n, unless there is a grammar rules in these things which you have not specified. Note that I replaced spaces with dots in this message, otherwise the message does not render correctly. const char * is just the type for C-style raw strings.
    – Fabio
    Nov 10 at 17:00











  • Ohhh I see. I just realized that the I didn't copy the string correctly so the spaces are off. But it works now. Is there a way to access individual elements by s[i][j]?
    – Josh Garza
    Nov 10 at 17:11










  • Also, how could I use the traditional nested for loops instead of the range-based for loops?
    – Josh Garza
    Nov 10 at 17:18










  • Yes, you can use v[i][j]. If you know the size in advance, do not use vector, a C-style array will do, it will simplify the example quite a bit. Yes, you could use normal loops. I see you are new to this web site. If you think this is the correct answer, you may consider up-voting it and marking it as correct.
    – Fabio
    Nov 10 at 17:24
















Wow, this is very helpful. I ran it on a online compiler very quickly and everything printed out correctly except the 2nd and 4th row. The second hashtag is in the 3rd column when it should be in the last column. Also, why did you use const char *s to store the string?
– Josh Garza
Nov 10 at 16:55




Wow, this is very helpful. I ran it on a online compiler very quickly and everything printed out correctly except the 2nd and 4th row. The second hashtag is in the 3rd column when it should be in the last column. Also, why did you use const char *s to store the string?
– Josh Garza
Nov 10 at 16:55












If you want it in the last column, then you need to add more spaces. The second line instance of being "#.#n" should be "# ....#n, unless there is a grammar rules in these things which you have not specified. Note that I replaced spaces with dots in this message, otherwise the message does not render correctly. const char * is just the type for C-style raw strings.
– Fabio
Nov 10 at 17:00





If you want it in the last column, then you need to add more spaces. The second line instance of being "#.#n" should be "# ....#n, unless there is a grammar rules in these things which you have not specified. Note that I replaced spaces with dots in this message, otherwise the message does not render correctly. const char * is just the type for C-style raw strings.
– Fabio
Nov 10 at 17:00













Ohhh I see. I just realized that the I didn't copy the string correctly so the spaces are off. But it works now. Is there a way to access individual elements by s[i][j]?
– Josh Garza
Nov 10 at 17:11




Ohhh I see. I just realized that the I didn't copy the string correctly so the spaces are off. But it works now. Is there a way to access individual elements by s[i][j]?
– Josh Garza
Nov 10 at 17:11












Also, how could I use the traditional nested for loops instead of the range-based for loops?
– Josh Garza
Nov 10 at 17:18




Also, how could I use the traditional nested for loops instead of the range-based for loops?
– Josh Garza
Nov 10 at 17:18












Yes, you can use v[i][j]. If you know the size in advance, do not use vector, a C-style array will do, it will simplify the example quite a bit. Yes, you could use normal loops. I see you are new to this web site. If you think this is the correct answer, you may consider up-voting it and marking it as correct.
– Fabio
Nov 10 at 17:24





Yes, you can use v[i][j]. If you know the size in advance, do not use vector, a C-style array will do, it will simplify the example quite a bit. Yes, you could use normal loops. I see you are new to this web site. If you think this is the correct answer, you may consider up-voting it and marking it as correct.
– Fabio
Nov 10 at 17:24


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240967%2fhow-to-create-a-2d-array-of-chars-given-a-string%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo