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];
c++ multidimensional-array graph maze
add a comment |
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];
c++ multidimensional-array graph maze
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
add a comment |
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];
c++ multidimensional-array graph maze
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
c++ multidimensional-array graph maze
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
add a comment |
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
add a comment |
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;
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 usev[i][j]
. If you know the size in advance, do not usevector
, 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
|
show 2 more comments
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;
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 usev[i][j]
. If you know the size in advance, do not usevector
, 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
|
show 2 more comments
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;
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 usev[i][j]
. If you know the size in advance, do not usevector
, 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
|
show 2 more comments
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;
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;
edited Nov 11 at 6:18
Prix
15.8k1252112
15.8k1252112
answered Nov 10 at 16:43
Fabio
823318
823318
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 usev[i][j]
. If you know the size in advance, do not usevector
, 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
|
show 2 more comments
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 usev[i][j]
. If you know the size in advance, do not usevector
, 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
|
show 2 more comments
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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