Thread: Character Manipulation

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    2

    Character Manipulation

    Hello
    I am to write a program that will drop a certain character from a string.
    Such as with the string "hi hun" I want the h to be dropped thus "i un". I'd also like to do it for uppercase letters as well. Here is my attempt thus far, only focusing on getting one case to drop rather than an uppercase as well.
    Code:
    #include <iostream.h>
    char remove(char[]);
    int main()
    {
    const int MAX = 80;
    char message[MAX];
    cout << "Enter a message:  " << endl;
    cin.getline(message, MAX);
    remove(message);
    cout << message << endl;
    
    return 0;
    }
    char remove(char mes[])
    {
    for(int i = 0; mes[i] != '\0'; i++)
    if (mes[i] = 'h')
    (mes[i] - 'h' + ' ');
    }
    The output I recieve is in all h'
    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    if (mes[i] = 'h') should be if (mes[i] == 'h'). = is assignment, == is comparison.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (mes[i] = 'h')
    You mean == for comparison, this is an assignment at present.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    In your if statement you want to use == (comparison) instead of = (assignment). And the line after that doesn't really do anything--you could just assign a space there.
    Code:
    if (mes[i]=='h')
      mes[i]=' ';
    But if you don't want extra spaces you'd have to do a bunch of shifting of array elements which could be slightly difficult.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    2
    Ah I feel silly.
    I went back and changed it. It fixed the hhhh thing but it's still not removing the character h from the string. Basically i'm putting it in and its coming back to me the same.
    Thanks for the input thus far.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    what you could try, is instead of simply assigning a space, is if it doesn't match the char you want to remove, you copy the character over to the new char array, other wise you simply skip it.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 08:11 PM
  4. LoadFromFile() from a txt
    By Snoopy104 in forum C++ Programming
    Replies: 6
    Last Post: 03-14-2006, 10:05 AM
  5. Manipulation of character arrays
    By Han in forum C Programming
    Replies: 4
    Last Post: 04-17-2002, 01:36 PM