Solved

Parsing a String in C

Posted on 2011-03-05
10
397 Views
Last Modified: 2012-06-27
I'm hoping this is going to be really simple, but for some reason I can't seem to make it work.

I'm working in Visual Studio 2010 Express, and I'm trying to write a really simple C++ form application.

Basically, I want the user to input a string into a text box, and then I'm going to split up that string that the user enters and display parts of it in various other text boxes on the form.

To be more specific, let's say that the user inputs "Hello, World!" into the text box and clicks OK (the clicking is what will initialize my data parsing).

I know how to access the ENTIRE string of "Hello, World!" very very easily, it's just like this:

(Note: I've labeled the text box txtbxMainText).

txtbxMainText->Text

Now, how do I pull out just "World" or just "or" or any other part of that string easily?

Does this make sense?
0
Comment
Question by:PGRBryant
10 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 35045163
I never did a VS managed form before; so I can only offer you what I tried out after reviewing msdn pages.
    http://msdn.microsoft.com/en-us/library/system.string.split(VS.71).aspx
   array<Char>^chars = {' ',  ',',  ':',  '.', '-', '|'};
   array<String^>^words = words2->Split( chars );

Open in new window

My form also had a Next Word text box, and every time I hit the OK button, the next word appeared in it. The next word would rotate around after the last word showed up by using a modulo index of
 words->Length

Open in new window

0
 
LVL 3

Expert Comment

by:gmleeman
ID: 35045791
if you were to use pure C, it would look quite different...Do you want to use C or C++ and all the modern MSVS methods?  Just curious because your Question Title uses 'C'...
0
 
LVL 1

Author Comment

by:PGRBryant
ID: 35062189
In this case I meant C++, my apologies. Since I'm working with the forms in Visual Express 2010.

Working with strings in C wasn't difficult to figure out, in my experience.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 32

Expert Comment

by:phoffric
ID: 35063479
Is the solution I provided what you were looking for?

Understand that when you are using Visual Studio 2010 Express to write a C++ forms application, then the code produced is not totally in the ANSI approved ISO/IEC 14882 C++ standard.

For that reason, I had to research this variant C++ language to come up with a working form. This variant C++ language provided the split function.
0
 
LVL 1

Author Comment

by:PGRBryant
ID: 35066613
Perhaps I don't understand what you did.

I have a really, essentially random, first piece of text that I recieve, and I"m trying to parse that data in forms.

I figured out how to do it with just simple C formatting, but I was hoping to be able to make it look more eloquent with a form-based approach.

The initial string is that is entered into the main text box is something like

%TRORANGE^WORLD$HAPPY$DAYS^17790301=4443950023

And I'm going to be doing things like pulling out the "ORANGE" and the "HAPPY", etc, and I need to individually pull those out... for example, in more simple C I could pull out the "TR" by just
mystr.substr(1,2);
0
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 35066749
This may give you some idea.

#include "iostream"
#include "sstream"

int main(){
  std::string s("Somewhere down the road"),s1,s2,s3,s4;
  std::stringstream ss(s);
  ss >> s1 >> s2 >> s3 >> s4;
  std::cout << "Substrings: " << s1 << "*" << s2 << "*" << s3 <<"*"<< s4 << std::endl;
  return 0;
}
0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 35066923
From your OP, I thought you were just trying to find the words entered into a text box which were delimited by a space (or other delimiters of your choosing). Here is the OK click method that I used:
   static int iNext=0;
   private: System::Void OK_Click(System::Object^  sender, System::EventArgs^  e) {
         String^ words2 = enterWordsBox->Text;
         int idx;
         array<Char>^chars = {' ',  ',',  ':',  '.', '-', '|'};
         array<String^>^words = words2->Split( chars );
         int modulus = words->Length;

         if ( modulus > 0 ) {
            while (1) {
               idx = iNext++ % modulus;
               if( words[idx]->Length > 0 ) {
                  nextWordTextBox->Text = words[idx];
                  break;
               }
            }
         }
         else {
            nextWordTextBox->Text = " ** PLEASE ENTER SOMETHING ** ";
         }
      }

Open in new window


>> for example, in more simple C I could pull out the "TR" by just:  mystr.substr(1,2);   I guess you meant simple C++ where substr is a method in the string class.
       http://www.cplusplus.com/reference/string/string/substr/

Here is code to illustrate a similar String.Substring Method taken from:
    http://msdn.microsoft.com/en-us/library/system.string.substring(v=VS.71).aspx
String *myString = L"abc";
bool test1 = String::Compare(myString->Substring(2,1), L"c") == 0;  // This is true.
myString->Substring(3,1);  // This throws ArgumentOutOfRangeException.
bool test2 = String::Compare(myString->Substring(3,0), String::Empty) == 0;  // This is true.

Open in new window

Again, this last bit of code uses libraries which are neither in standard C nor C++ , but useful if you want to use VS 2010 Express forms. (I don't have experience with this language, so I cannot say whether it is used in other systems.)
0
 
LVL 1

Author Closing Comment

by:PGRBryant
ID: 35287443
I apologize pho, you definitely put in the effort and deserve the points, I got distracted with other projects and didn't come back to verify your work.

Thanks!
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35288273
No apologies necessary. But considerate of you to do so. I understand that busy people are, well, busy. I figured you would get around to this when you had time.

But just to clarify, you don't have to accept a post if you have questions about it remaining. What counts is your satisfaction, and not the effort we put into it. So, if you have questions about anything related to this question, please do not hesitate to ask here.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB6 Compile Compatibility Issue 4 102
Device same like our heart 12 82
c++ reading data from file into two dimensional array 3 107
iSeries email authority 6 56
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question