Thread: strings

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    5

    strings

    Hi! I'm pretty new to C so this might seem like a dumb question.
    I need to compare a string (ex: string=abcde) and compare it with one character (ex: char=c) and if there is a character in the string that is the same as "c", take that character out and return the string "cleaned" if you will.
    So the program would give back "abde".
    I was wondering if there is a way to suppress(I don't know if thats how you say it) a character in a string. So like if I were to say

    for(i=0;i<5;i++)
    if (string[i]==char)
    string[i]="/0"

    or something like that.
    Thanks!

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    What I would do is write a loop that steps through every element of the string, and if it _isn't_ that character you are looking for, copy it to another string.

    So you'll need two indexes to keep track of. Where you are in the original string, and where you are in the target string.

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Ya wut he said...

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    I suppose you could get creative with strchr() or strtok().

    Edit: Or use memmove() after locating the character
    Last edited by itCbitC; 03-12-2009 at 09:02 PM.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I suppose you could get creative with strchr() or strtok().
    Ooh yes - strtok in a loop with strcpy would be a very neat algorithm in code...

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, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08: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