1

How can you convert a 2d char array into a string?

 int main()
{
   char foo[3][3] = {{'a','b','c'}, {'d','e','f'},{'g','h','i'}};
   string bar;
   bar = foo;
   cout<< bar; //abcdefghi

 return 0;
}

also can you convert only select parts of a 2d array to a string?

 int main()
{
   char foo[3][3] = {{'a','b','c'}, {'d','e','f'},{'g','h','i'}};
   string bar;
   bar = foo[0][1] + foo[1][2] + foo[2][0];
   cout<< bar; //bfg (bar contains only b, f, and g)

 return 0;
}
6
  • 6
    Instead of only showing what you want, please show us what you have tried. Commented Feb 27, 2014 at 7:47
  • also can you convert only select parts of a 2d array to a stringcan you explain this a lil more Commented Feb 27, 2014 at 7:50
  • Iv spent the last 2 hours trying strcpy,strcat,strncat,memcpy and every other possible thing I could think of and nothing works. Iv tried googleing for it and there are thousands of "convert string to 2d array" tutorials but I cant find a single one showing how to do 2darray to string. Commented Feb 27, 2014 at 7:51
  • 'trying strcpy,strcat,strncat,...' Those may have failed because of the missing '\0' terminators in the 2nd array level. Commented Feb 27, 2014 at 7:54
  • +1 for spending 2 hours :) Commented Feb 27, 2014 at 8:05

4 Answers 4

7

You can use the fact that the elements in the 2D array are contiguous, and the two-iterator constructor of std::string:

char foo[3][3] = {{'a','b','c'}, {'d','e','f'},{'g','h','i'}};
std::string bar(&foo[0][0], &foo[2][2]+1);
std::cout << bar << std::endl; // abcdefgi

Here, &foo[0][0] is a pointer to the first element, and &foo[2][2] + 1 is a pointer to one past the last one. And pointers are iterators.

Sign up to request clarification or add additional context in comments.

8 Comments

can you explain why not &foo[2][2] only?
@OmerObaid The second iterator is never included. For a given range, the second iterator is "one past the last". So &foo[2][2] would only copy abcdefgh.
@UnTraDe It is more that I am too lazy to write my own loops :-)
I was interested in why you did the &foo[2][2]+1 instead of just &foo[2][2] and I found this: Input iterators to the initial and final positions in a range. The range used is [first,last), which includes all the characters between first and last, including the character pointed by first but not the character pointed by last. Source: cplusplus.com/reference/string/string/string Might help others who try to figure this out.
@UnTraDe It is a common idiom in the standard library. I added a link to a std::string constructor reference. I think all the information is there.
|
2

hope this piece of code will help, welcome to c++

#include <iostream>
using namespace std;
int main()
{
   char foo[3][3] = {{'a','b','c'}, {'d','e','f'},{'g','h','i'}};
   string bar;
   bar = "";
   for(int i =0 ; i< 3;i++)
   {
      for(int j =0 ;j<3;j++)
      {
          bar += foo[i][j];
      }
   }
   cout<< bar; //abcdefghi

 return 0;
}

5 Comments

I wouldn't use using namespace; no need from bar = "";; you can use std::string::reserve to increase performance; I'd use std::string::append instead. Just opinion.
I had no idea you could simply set a string equal to a char array. Thank you.
@user3331346 'set a string equal to a char array' That's not what the add operation (bar += foo[i][j];) does!
@πάνταῥεῖ I understand the += is appending the value of foo and not setting bar equal to the value of foo. What I meant was I was under the impression that the only way to convert a char to a string was to use some function like strcpy. I did not think it would be possible to just string = char. I am very new to C++.
@user3331346, it is actually the operator +=, and it char each time it is added see the definition here cplusplus.com/reference/string/string/operator+=
0

The C++ standard does not contain a function to this, so you'll have to write your own.

You can use the following code:

int main(int argc, char const *argv[])
{
    char foo[3][3] = {{'a','b','c'}, {'d','e','f'},{'g','h','i'}};
    string bar;


    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            bar.append(foo[i][j]);
        }
    }

    cout << bar;

    return 0;
}

This code iterate through the 2d array, which is acually an array or arrays, and add each character to the string.

Comments

0
int main(){

 Char ar[4][10]=. 
  {"one","two","three","four"};


      for(int i=0;i<4;i++)
      {
           Cout<<ar[i]<<endl;

      }

return 0;
}

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.