strings

This is a discussion on strings within the C Programming forums, part of the General Programming Boards category; Hi! I'm pretty new to C so this might seem like a dumb question. I need to compare a string ...

  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
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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
    398
    Ya wut he said...

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    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 10:02 PM.

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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, 07:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 03:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

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