Creating Strings from char arrays

Hello! I have a problem that needs me to load a char array from a file, make every line of that array into a string, and then sort the strings.

EX:

bgtf
bhgy
acfg
cfgg

needs to turn into

acfg
bgtf
bhgy
cfgg


I have the character array loaded fine, I am just not sure how to turn it into four different strings that I can then sort?

The actual problem uses a [10][15] array. So I was trying to figure out a way to use a for loop to create the strings. I think I may just be ignorant to some part of string properties. I have all the libraries included.

Bottom line for now, I just need to figure out how to "load" a new string value with a range of chars.

Thanks!

To illustrate a little more... in my mind I am trying to do this:

string line1(array[0][0-3]) in order to get line1="bgtf" (from the top array)

Obviously this isn't working for me, and I am pretty sure I am just doing it wrong and lack the proper syntax.
std::string has a constructor that takes a c-string so string line1(array[0]);

If you want to sort the strings pit them in an array or vector.
1
2
3
4
5
6
std::vector<std::string> lines;
//assuming you keep track of how many strings are loaded in SIZE
for(int i = 0; i < SIZE; ++i)
    lines.emplace_back(array[i]);
...
std::sort(lines.begin(), lines.end());

Last edited on
Thank you.

So, what I am missing here is how to load just one line into the string, or in my example above, 4 chars.

If I create string line1(array[0]);

line1 ends up being equal to the entire file that I am trying to look at. So I need to know how to tell the program to only read in one line, or 15 characters (4 in my example).

I also had some success with fin.getline(line1, 15);

That ended up being what I wanted. I am just not sure how to make line2 equal the next 15 characters, or the next line?
closed account (j3Rz8vqX)
Can you use vectors? or just arrays?
A vector solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//...
    vector<string> str;
    string temp;
    fstream file("data.txt",ios::in);
    if(file.is_open())
    {
        while(getline(file,temp))
            str.push_back(temp);
        file.close();
    }
//...
Before sort:
hello world
this is a string
the string is short
omg
bye

After sort:
bye
hello world
omg
the string is short
this is a string
Press <enter> to exit console:


An array solution would be to make an array large enough to handle the possible size being read and assign the data to an element; keeping track of how much of the element is actually being used.

OR

Dynamic pointer arrays; which requires a bit more work - I do not advise it, unless you are strong with dynamic memory control.
It seems my instructor is pretty insistent on utilizing a character array. The character array is [10][15].

I need to load the array from a file, which I've done.

I thought I could just convert each line of the array into a string, sort the strings and be done with it. But I can, he wants the same routine that prints the original array to print the sorted array.

I have figured out how to turn them all into strings, and I imagine ill be able to sort the strings. What I do not know how to do, is convert the 10 strings back into a character array that can be printed by the original print function that read the file. But I am sure there is a way.

So if I have string line1, line2, line3, line4 from my original example: After its sorted, it would need to display in the order of line3, line1, line2, line4. So I need to reload a char array in that order. How would I do that.



Or is there some clever way to sort only the order precedence of the rows in a two dimensional char array without ever converting it to strings?

Thank you so much in advance. This one really has me scratching my head.

We have barely touched vectors and have not touched pointers. So I dont think I am supposed to use either.
If you are using character arrays rather than std::string, you might do things using the functions which work with c-strings, such as strcpy() to copy a string, and strcmp() to compare two strings. That way you might tackle this without using std::string or std::vector and so on.

http://www.cplusplus.com/reference/cstring/strcpy/
http://www.cplusplus.com/reference/cstring/strcmp/
Yes, that is what I think will work in the end.

I am just lacking the syntax to know how to turn a char array into strings to compare, and then turn the sorted strings back into a char array to print.

So example pseudo code of what I want to do is:

char ary[2][9] (30, in 3 elements of 10)

turn this array into three strings:

string line1, line2, line3,

sort the three strings into alphabetical order

NOW, turn the three strings back into a char array of 3 elements of 10.
How do I do that?

I have little experience with strings, so imagine the answer isnt too difficult, I just lack the syntax knowledge.

In my mind, I want to do this, ary[0]={line1}
This doesnt work though. How would I do that? lol
Well, a character array is already a string.
For example the character string "Hello" is effectively the same as
char array[6] = { 'H', 'e', 'l', 'l', 'o', 0}; (though there are differences).

In the case of a 2-D array such as char strings[10][15];, it can be considered as 10 occurrences of a character string of length 15, including the null terminator.

Example, here the first three of the 10 strings are used:
1
2
3
4
5
6
7
    char array[10][15] = {
        "One", "Two", "Three"
    };
    
    cout << array[0] << endl;
    cout << array[1] << endl;
    cout << array[2] << endl;
One
Two
Three


I want to do this, ary[0]={line1}

I don't know what you mean by line1 there. Also I don't know whether you want to set an initial value for ary[0] or to assign a different value during execution.

As I mentioned previously, strcpy() can be used if you want to copy one string to another.

Tutorial:
http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/doc/tutorial/ntcs/

Last edited on
Thank you. That helped.
Topic archived. No new replies allowed.