#1
  1. Happy Monkey
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Location
    UK (University of Kentucky)
    Posts
    1,809
    Rep Power
    47

    How do I "trim" a char array?


    Is there a library that contains a function to trim a char array? In PHP the trim() function removes any white space or newline chars, or carriage returns... etc. from the beginning and end of a string.

    The reason is I have a file set up like this:
    Code:
    name_one            Sam
    name_two            Fred
    name_three           Bob
    I am opening the file and cout-ing name_one and asking the user for input. Then I compare what they typed in to Sam. But it doesn't ever match, so I am assuming that it is the \n character that is being past from the user, and that's why I want to trim it. Here is a snippet of my code:
    Code:
      ifstream OpenFile("cpp-test.txt");
      static char ch[50];
      static char chin[50];
      static char user[50];
    
      while(!OpenFile.eof())
      {
        OpenFile.getline(ch,50,'\t');
        cout<<ch<<": ";
        OpenFile.getline(chin,50,'\n');
        cin>>user;
        if(user == chin)
        {
          cout<<"GOOD JOB! That is correct!\n\n";
        }
        else
        {
          cout<<"WRONG!   "<<chin<<"\n\n";
        }
      }
      OpenFile.close();
  2. #2
  3. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    28
    >>if(user == chin)

    What is contained in the strings here?

    test they contain what you think.

    try a

    if(strstr(user,chin)!=NULL)

    to see if both are similar then print them out.

    or add the '\n'

    something like

    chin[lstrlen(chin)]='\n';
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  4. #3
  5. Happy Monkey
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Location
    UK (University of Kentucky)
    Posts
    1,809
    Rep Power
    47
    Thanks man!

    if(strstr(user,chin)!=NULL)

    That worked like a friggen charm! :D So now, does strstr() just compare two strings by ignoring the new line char? I am coming to C++ from PHP, and PHP can compare two strings with a simple ==.

    I also tried chin[lstrlen(chin)]='\n'; to see if that worked also, but it started complaing about not including the correct library. Which one do I need for lstrlen()? Thanks again for your help. :)
  6. #4
  7. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    28
    sorry, my bad.

    In C++ you can use the == for strings but not in C.
    I prefer to use the string functions (as I learnt C first)
    My understanding is that it == will compare ALL the eements of the array including teminators ect causing your problem.

    to start with

    strstr() only finds occurences of one string in another. I expected you to just use it as a test....

    ie turn is in return and would match.
    the return is apointer to the first occurence or null if no match.

    strcmp() will return 0 if both exactly equal. It also has version that ignore case ect.

    if(strcmp(user,chin)==0)//match

    all these come with a windows (WIN32) version preceeded with an l ie lstrcmp()

    so use

    strlen() not lstrlen()

    all are from string.h

    isalpha(), isspace() isdigit() ispunct() will help in triming the strings if needed.
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  8. #5
  9. Happy Monkey
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Location
    UK (University of Kentucky)
    Posts
    1,809
    Rep Power
    47
    Hey man thanks! This is some great information! As always I have a few more Q's for you. :D

    I change my if to

    if( !strcmp(user, chin) )

    like you suggested. So let me get this stright. strcomp() returns false on match, but true on fail?

    And as for the other functions:

    isalpha() // Removes alph-numberioc characters?

    isspace() // Removes spaces?

    isdigit() // Removes numeric chars?

    ispunct() // Removes punctuation?

    I have tried using these functions and it gives me an error:

    MakeFile.c:46: incompatible types in assignment of `int' to `char[50]'

    Can these only be used with ints?

    Sorry to keep asking questions. Is there a good site out there with a searchable database of C++ functions like PHP has @ www.php.net? Thanks again!
  10. #6
  11. Happy Monkey
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Location
    UK (University of Kentucky)
    Posts
    1,809
    Rep Power
    47
    Never minnd about the compile error I got. I was doing something stupid. :D

    Is this invalid syntax?
    Code:
      cout<<"isalpha(): "<<isalpha(test)<<endl;
      cout<<"isspace(): "<<isspace(test)<<endl;
      cout<<"isdigit(): "<<isdigit(test)<<endl;
      cout<<"ispunct(): "<<ispunct(test)<<endl;
    When I run the proggie, and get down to that, it says "Segmentation fault"
  12. #7
  13. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    strcmp() returns an integer value representing which argument is greater than the other. A value of 0 (zero) means that they are equal, and positive value means that the left is greater and a negative means that the right is greater.

    The other functions are meant to be tested on a single character and return 0 or 1. They are tests and do not alter the character passed to it.
    isalpha() tests to see if the character is from A to Z
    etc...

    you use it like:
    Code:
    char test='a';
    if (isalpha(test)) {
        cout << test << " is alpha\n";
    }
  14. #8
  15. Happy Monkey
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Location
    UK (University of Kentucky)
    Posts
    1,809
    Rep Power
    47
    Thanks. :)

IMN logo majestic logo threadwatch logo seochat tools logo