Help with problem ( strings )

This is a discussion on Help with problem ( strings ) within the C++ Programming forums, part of the General Programming Boards category; Okay, my problem is I can't figure out how to do this... I want to take a string and parse ...

  1. #1
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    Help with problem ( strings )

    Okay, my problem is I can't figure out how to do this... I want to take a string and parse through it, taking out all singular words, seperated from other words by spaces.

    so

    NORTH SOUTH UP DOWN

    And the program would be able to take out and recognize NORTH, SOUTH, UP, and DOWN as individual words and make actions based on those. Here is some code, messy code...:

    ( sorry for making the class all public, but I'm just doing it for test purposes )

    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    
    using namespace std;
    
    class Test
    {
    public:
    	string	usercmd;
    	Test (string u) : usercmd(u) { }
    	void		disp (void) 
                {
    	       cout << usercmd << endl;
                }
    	void		set_caps (int pos = 0);
    	int			get_sloc (int pos = 0);
    
    	void		translate_cmd (void);
    	void		tran_to_dat (string &t, int l);
    };
    
    void Test::translate_cmd (void)      
    {
    	set_caps();
    	int fs = 0;
    	int ns = get_sloc();
    	string sub;
    	int loc = 0;
    	for (loc = 0; ; ++loc)
    	{
    		sub = usercmd.substr(fs, ns);
            tran_to_dat (sub, loc);
    		fs = ns + 1;
    		ns = get_sloc(fs);
    
    		if (fs > usercmd.size() )
    			break;
    	}
    
    }
    
    void Test::tran_to_dat (string &t, int l)
    {
    	int val = -1;
    	if (t == "NORTH")
    		val = 256;
    	if (t.substr(0, t.size()) == "SOUTH")
    		val = 255;
    	if (t == "WOWOW")
    		val = 512;
    	cout << "pos: " << l << "  ~" << t << "~  ";
    	cout << val << endl;
    	Sleep(1500);
    }
    
    int Test::get_sloc (int pos)			// find first space from pos
    {
    	++pos;			// increase pos to find correct space
    	for (int i = pos; i < usercmd.size(); ++i)
        {
    		if (usercmd[i] == ' ')
    			break;
        }
    	
    	return i;
    }
    
    void Test::set_caps(int pos)			// turn string to all-caps
    {
    	 for (int i = pos; i < usercmd.size(); ++i)
         {
    		if ( (int)usercmd[i] > 96 && (int)usercmd[i] < 123 && usercmd[i] != ' ') 
    			usercmd[i] =  usercmd[i] - 32 ; 
         }
    }
    
    
    int main()
    {
    	Test MyTest("North South Up Down");
    	MyTest.disp();
    	MyTest.translate_cmd();
    
    	return(0);
    }
    run this code and you'll notice it parses it as NORTH (first word is correct), SOUTH UP DO (incorrect), UP DOWN (incorrect), and DOWN (correct, but...)

    Any suggestions on how to get it working? I appreciate any help... = ]

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Try something like -

    Code:
    ##include <iostream>
    #include <string>
    #include <algorithm>
    #include <cctype>
    #include <windows.h>
    
    using namespace std;
    
    class Test
    {
    public:
    	string	usercmd;
    	Test (string u) : usercmd(u) { }
    	
    	void disp(void){
    	       cout << usercmd << endl;
        }
    	void set_caps ();
    		
    	void translate_cmd (void);
    	void tran_to_dat (string t);
    };
    
    void Test::translate_cmd (void)      
    {
    	set_caps();
    	string::size_type a=0;
    	string::size_type delim=0;
    
    	while(delim!=-1)
    	{
    		delim = usercmd.find_first_of(" ",a);
    		tran_to_dat(usercmd.substr(a,delim-a));
    		a=delim+1;
    	}
    	
    }
    
    void Test::tran_to_dat (string t)
    {
    	int val = -1;
    	if (t == "NORTH")
    		val = 256;
    	if (t == "SOUTH")
    		val = 255;
    	if (t == "WOWOW")
    		val = 512;
    	cout << "pos: " <<  "  ~" << t << "~  ";
    	cout << val << endl;
    	Sleep(1500);
    }
    
    
    void Test::set_caps()			// turn string to all-caps
    {
    	transform(usercmd.begin(),usercmd.end(),
    		usercmd.begin(),toupper);
    }
    
    
    int main()
    {
    	Test MyTest("North South Up Down");
    	MyTest.disp();
    	MyTest.translate_cmd();
    
    	return(0);
    }

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >Either ignore this post

    I would, or fix the memory leaks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 09:01 PM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  3. Problem with Strings and Conversions
    By patso in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 01:01 PM
  4. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 01:07 PM
  5. copy strings problem
    By dgcampos in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2004, 09:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21