Thread: How to write code for separating words from a string ?

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    4

    Question How to write code for separating words from a string ?

    May I know how to write a c++ program where a string value is to be read in from the keyboard and the program should generate an output in the following format :

    Example : Mr Brown*246 The Park Building*Neopets Area*Country

    Output :
    Mr Brown
    246
    The Park Building
    Neopets Area
    Country

    May I know what code to use to write the above program? I did try to use 'strtok' to write but it only return to a new line whenever it see a spacing and not a * .
    Can anyone please enlightened me on this problem ?
    Thank You.

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802

    Re: How to write code for separating words from a string ?

    Originally posted by dingtang

    Example : Mr Brown*246 The Park Building*Neopets Area*Country

    Output :
    Mr Brown
    246
    The Park Building
    Neopets Area
    Country

    Try using the CString class, itt'l make things a lot easier for you.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    4

    Question

    Thank you vVv for your reply. But when I run your program, my program show me a error message ' Undefined symbol 'separated'.
    May I know why ?
    I am using Borland Builder 5.

    Thanks

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    4
    Thank you vVv. I can run the program now but how come the output is different from what I expected ? I only want a new line whenever the code detected an * . May I know how to improve on it ?

    Thank you very much .

  5. #5
    Unregistered
    Guest
    Ah, I'm dumb- Read your question absent-minded. Well, just change

    if( text[i] != '*' /*no && != ' ' here*/ )

    and

    for( i =0; i < 4; i++ )

    >I only want a new line whenever the code detected an *

    Then your example misses a '*' after 246.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Location
    computers/ theatre, travelingstudentL>- Northern California NativeNorthern California3D&Tie-DyeStudent1>. Admirer of C Bangalore Net surfingTeacher >/ >0 nagpurteaching >1  >2
    Posts
    61
    Here is an example written in java that will read a string from a file and return an array of strings. It should be easy to adapt it to C /C++.

    Code:
    import java.net.*;
    import java.io.*;
    
    public class ParsingStreamReader
    {
      BufferedReader file;
    
      public ParsingStreamReader(InputStream in)
      {
        file= new BufferedReader(new InputStreamReader(in));
      }
    
      public String readLine()
      {
        try
        {
          return file.readLine();
        }
        catch(Exception e)
        {
          return null;
        }
      }
    
      public String[] parseLine()
      {
        String ps[]=new String[0];
        String str;
        String tmp[]=null;
        int front=0,end=0;
    
        try
        {
          str=file.readLine();
        }
        catch(Exception e)
        {
          return null;
        }
        if(str==null)return null;
        str=str.trim(); //Remove white space from the front and back of the string.
        if(str.length()==0)return ps;
    
        while((str.charAt(end)>' ')&&(end<str.length()-1))end++;
    
        while(true)
        {
          tmp=ps;
          ps=new String[ps.length+1];
          for(int x=0;x<tmp.length;x++) ps[x]=tmp[x];
          ps[ps.length-1]=str.substring(front, end);
          if(end>=str.length()-1)break;
          if(end<str.length()-2)
          while((str.charAt(end+1)<=' ')&&(end<str.length()-1))end++;
          front=end+1;
          end=front;
          if(end<=str.length()-1)
          while((str.charAt(end)>' ')&&(end<str.length()-1))end++;
          if(end==str.length()-1)end=str.length();
        }
        return ps;
      }
    }

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    4
    Thank you very much for all your help. I still have a question, how to write the code where the user input the sentence ? For example there will be a question asking the user to enter a sentence and then the program will separate the sentence into different new line.

    Is it using printf("enter text:") ?

    But if I use printf , how do i amend the rest of the code that vVv had written ?

    Please enlightened me. I am totally new to C programming and I am very grateful to those that written the above message to me.

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Replies: 3
    Last Post: 03-27-2004, 12:15 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM