Trim A String

This is a discussion on Trim A String within the C++ Programming forums, part of the General Programming Boards category; Hi, as you may no i am working on some serial communications code, to communicate with my elecronics project. Here ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    16

    Trim A String

    Hi, as you may no i am working on some serial communications code, to communicate with my elecronics project.

    Here is the deal, i have a string:

    s654us23us456us234us2us34

    Now, the string will always be 2 letters followed by some numbers, but here may be 1, 2 or 3 numbers depending on what is happening.

    I can split a string like this:

    us23us456us234us2

    Into arrays. BUT my problem is with the strings in that the end part may be cut off as with the start.

    So if the first 2 characters are letters then the start is fine. If not then the first letter (if there is one) and numbers must be deleted so that the first 2 characters are letters.

    The end is different, if a number is present as the last character:

    all numbers deleted in reverse order (going back through the string), and then deleting the 2 letters.

    If a letter is present at the end then just the letters at the end need deleting.

    I have tried myself to do this, but i am within weeks of starting C++ so need any help possible.

    I hope i have explained things well enough for you to understand and help me.

    Thank You,

    - Martyn

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Take a look at this code for the start of the string:

    Code:
    #include <iostream>
    #include <string>
    #include <ctype.h>
    using namespace std;
    
    int main()
    {
      string sVar = "s6us23us456us234us2us34";
      cout << "Before = " << sVar << endl;
    
      if ( isdigit(sVar.at(0)) || isdigit(sVar.at(1)) )
      {
        int count;
        for(count=2; count < sVar.length(); count++)
            if ( isalpha(sVar.at(count)) )
                break;
        
        sVar.erase(0, count);
      }
      cout << "After = " << sVar << endl;
      system("PAUSE");
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    16
    Thanks,

    Thats is very helpful.

    How would i got about doing the end bit? Counting backwards for example?

    The end bit is the most important becasue us202 could be sent but only us2 recieved, there is a large difference between 2cm and 202cm

    Thanks

    - Martyn

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    16
    Oh and here is the code for the begining:

    Code:
    #include <iostream>
    #include <string>
    #include <ctype.h>
    using namespace std;
    
    int main()
    {
      string sVar = "u2us23us456us234us2us34";
      cout << "Before = " << sVar << endl;
      
      if ( isalpha(sVar.at(0)) && isalpha(sVar.at(1)) )
      {
      
      
      
      }else{
      
        if ( isdigit(sVar.at(0)) )
        {
        
            int i;
            while ( isdigit(sVar.at(i)) )
            {
            
                    i++;
                    
            }
            
            sVar.erase(0, i);
            
        }
        
        if ( isalpha(sVar.at(0)) )
        {
        
            int k;
            while ( isalpha(sVar.at(k)) )
            {
            
                    k++;
                    
            }
            
            while ( isdigit(sVar.at(k)) )
            {
            
                    k++;
                    
            }
            
            sVar.erase(0, k);
            
            
        }  
        
      }
      
      cout << "After = " << sVar << endl;
      system("PAUSE");
      return 0;
      
    }

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    This should get you going:
    Code:
      int len = sVar.length();
      if ( isdigit(sVar.at(len-1)) )
      {
         cout << "last char is a number..." << endl; 
      }
      else if( isalpha(sVar.at(len-1)) )
      {
       cout << "last char is a letter..." << endl;
      }
    Take a look at this page : http://www.cppreference.com/cppstring.html to see how to manipulate a string.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    16
    Thanks,

    I will post my *final* code in a few mins, when its final

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 05:05 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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