strings

This is a discussion on strings within the C++ Programming forums, part of the General Programming Boards category; 1) how do i convert C++ strings to upper case/lower case? 2) say if i got two strings extracted from ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    10

    strings

    1) how do i convert C++ strings to upper case/lower case?

    2) say if i got two strings extracted from a paragraph,
    pepper?
    pepper.

    how do i tell my program this 2 are the same strings?

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    1) you have to change each char in the string to upper or lower case one at a time There is a thread about this topic on page two of the board started by Asjan. Preludes post at the bottom of the thread gives one version of how to do this if the strings are STL string objects. The other posts offer other ways to do it, too. I think the link below will get you there.


    Strings
    2) you need to compare the strings. If they are C-type null terminated char array strings then you use strcmp() function from the cstring header file (or string.h header file if your compiler isn't up to date). If the strings are STL string class objects, then you can use the equals operator.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    10
    the problem is when i use the = sign, they treat the two strings as different

    eg. peter pepper pick a peck of pepper.

    the program will extract out

    pepper
    pepper. <- notice with a fullstop here.

    how do i tell the program these tweo strings are same

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    If you are concerned about the '.' character making your two strings unequal, then it should be easy to remove any punctuation or just specific types of punctuation prior to the comparison in order to make the comparison work as you want it to. You can use the remove to remove just a single type of punctuation or if you want to eliminate every form of punctuation then use remove_if.

    Code:
    #include <algorithm>
    #include <string>
    #include <cctype>    // For 'ispunct'
    using namespace std;
    
    ...
    
    string str;
    
    ...
    
    // Either this to remove just '.' for example...
    
    str.erase(remove(str.begin(),str.end(),'.'),str.end());
    
    // Or this to remove all punctuation from the string...
    
    str.erase(remove_if(str.begin(),str.end(),ispunct),str.end());
    Last edited by hk_mp5kpdw; 04-01-2004 at 11:26 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    beware, the = sign is the assignment operator, if the lhs has already been declared and the intialization operator if this is the declaration statement, and not the equals operator, which is ==. You can not use either the assignment operator or the equals operator for C-style strings, but you can use both for STL strings.


    If you are using C-style strings rather than STL strings, then you can use isalpha() or strtok() to help strip away any punction marks from the strings before comparison. I'm not sure, but I think the remove() functions only work for STL strings.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Quote Originally Posted by elad
    If you are using C-style strings rather than STL strings, then you can use isalpha() or strtok() to help strip away any punction marks from the strings before comparison. I'm not sure, but I think the remove() functions only work for STL strings.
    I've tried it with straight c-style arrays and it works:
    Code:
    char array[] = "H.!ell,o' ;Wor?ld:"; // 19 chars long including NULL
    cout << array << endl;
    remove_if(array,array+19,ispunct);
    cout << array << endl;
    Outputs:
    Code:
    H.!ell,o' ;Wor?ld:
    Hello World
    sizeof(array) is still 19 after the remove_if operation but the NULL gets copied to the new effective end of the punctuation free portion of the string... i.e. if you tried to do this:
    Code:
    cout << array+12 << endl;
    Then you would get the rest of the array:
    Code:
    or?ld:
    Last edited by hk_mp5kpdw; 04-01-2004 at 01:30 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And some more, probably, as the terminator is missing.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    thanks, hk_mp5kpdw. I don't use any of the STL algorithm functions yet. Time to get on the ball I guess.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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