473,394 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

converting string to char

hey everyone, i have a question. I am writing a program that sorts a list of lines of text. I read in each line of text into a string class string, manipulate that string, then once it is manipulated I need to save it to an array to sort later. Im trying to figure out how to take the string I have after it has been cleaned up, and put it into one element of an array. for example i want to do this string1 = array[0]. so that each individual element of the array[] would contain a whole line of text, so then i could sort (alphabetize) that array. can anyone help me.

ive tired tons of things but cant get the whole line of text from the string into a place where i can compare the different lines with each other to sort.

thanks
rjr
Feb 16 '07 #1
3 1832
sicarie
4,677 Expert Mod 4TB
hey everyone, i have a question. I am writing a program that sorts a list text. I read in each line of text into a string class string, manipulate that string, then once it is manipulated I need to save it to an array to sort later. Im trying to figure out how to take the string I have after it has been cleaned up and put it into one element of an array. for example i want to do this string1 = array[0]. so that each individual element of the array[] would contain a whole line of text, so then i could sort (alphabetize) that array. can anyone help me.

ive tired tons of things but cant get the whole line of text from the string into a place where i can compare the different lines with each other.

thanks
rjr
What have you tried so far? If you post what you have (and any error messages), we can help you with that and explain what happened with it.
Feb 16 '07 #2
I have tried a few different things. I have tried something like this:
where t is the t is the array i would sort later and s is the original string
Expand|Select|Wrap|Line Numbers
  1. const char *t;
  2. t[n] = s.c_str();
I tried using s.insert to insert all of s into another string t, but into only one element of t so like t[1].
Feb 16 '07 #3
Ganon11
3,652 Expert 2GB
I have tried a few different things. I have tried something like this:
where t is the t is the array i would sort later and s is the original string
Expand|Select|Wrap|Line Numbers
  1. const char *t;
  2. t[n] = s.c_str();
I tried using s.insert to insert all of s into another string t, but into only one element of t so like t[1].
Right now, t is a pointer pointing to an array of characters. So t[n] is trying to access a specific character in that array, but you are setting that element to an array of characters (a CString). You should use a double pointer to implement a 2D array of char values:

Expand|Select|Wrap|Line Numbers
  1. char **t;
  2. // For C++ (with new/delete operators)
  3. t = new char*[NUM_VALUES]; // Total number of strings to sort
  4. // For C using malloc (I think this is how it works)
  5. t = (char**)malloc(NUM_VALUES * sizeof(char*));
  6. // Either way, this should work:
  7. t[num] = myString.c_str();
This way, t[num] is an element in the array t - that element is a char pointer, which can point to another array of characters, in this case myString represented as a native char array.
Feb 16 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
4
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
18
by: Marcio Kleemann | last post by:
I need to force the first letter of each word in a line of text to uppercase. The text comes from a TextBox control in a Web Form. I'm new to ..NET and am having a problem. Since I can't modify...
3
by: fakeprogress | last post by:
How would I go about converting this C code to C++? /* LIBRARY is an array of structures */ /* This function compares 'tcode' with */ /* existing codes in the array. */ /* It...
2
by: pookiebearbottom | last post by:
Just looking for opinion on which of the 3 methods below people use in their code when they convert a 'const char *' to a 'const std::string &' came across #3 in someone's code and I had to...
0
by: anide | last post by:
Hi all I’ve some problem, I’m trying to converting a sorting algorithm from C++ to C#. In C++ I’ve compiled it using MSVC and its working properly, and in C# I’m using .NET Framework 2.0 (Visual...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
5
by: Hans Mull | last post by:
Hi! How can I convert a string to a const unsigned char*? (string::c_str() converts the string to a signed char) Thanks in advance, Hans
1
by: vcbytes | last post by:
I am having a problem with the following code: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { String^ texts = textBox1->Text; char *text =...
4
by: screamer81 | last post by:
Hello, I've a SDK functions description for a scanner and I try to convert unmanaged DLL C++ functions to c#. I converted all except one of them. This function is getting a struct parameter. ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.