Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Locating chars in strings etc.

Locating chars in strings etc.

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CreepingFeature
    wrote on last edited by
    #1

    It's a bitch! :mad: Anyhow here is the thing I got myself a string, just a word. I need to locate a char in it and then erase it, also it must be done in a loop so that if char is in there twice etc. it would be deleted. Problem: What if char doesn't exist in the string. I tried to use if( !(blahblah.find(char)) ) doesn't work because if .find doesn't see the char it throws the value of char position out of range of length of string or drops it bellow zero, causes an out of range error. (This took me half a day to figure out) :mad: So I used: if( (blahblah.find(char) < 0) || (blahblah.find(char) => Length) ) same crap! Two hours later found strpbrk()! How can I use it together with strlen to simplify the condition and avoid out of range error during run-time. Basicaly what I need is to look at a string, single word. Check if it has a char I provide. If it doesn't I need the if-statment to fall thru. But if it does have it I need to erase it, this I can do :laugh:.

    N D 2 Replies Last reply
    0
    • C CreepingFeature

      It's a bitch! :mad: Anyhow here is the thing I got myself a string, just a word. I need to locate a char in it and then erase it, also it must be done in a loop so that if char is in there twice etc. it would be deleted. Problem: What if char doesn't exist in the string. I tried to use if( !(blahblah.find(char)) ) doesn't work because if .find doesn't see the char it throws the value of char position out of range of length of string or drops it bellow zero, causes an out of range error. (This took me half a day to figure out) :mad: So I used: if( (blahblah.find(char) < 0) || (blahblah.find(char) => Length) ) same crap! Two hours later found strpbrk()! How can I use it together with strlen to simplify the condition and avoid out of range error during run-time. Basicaly what I need is to look at a string, single word. Check if it has a char I provide. If it doesn't I need the if-statment to fall thru. But if it does have it I need to erase it, this I can do :laugh:.

      N Offline
      N Offline
      Nigel Savidge
      wrote on last edited by
      #2

      if (strchr (blahblah, char)) eraseit ; Is this what you are looking for?

      C 1 Reply Last reply
      0
      • C CreepingFeature

        It's a bitch! :mad: Anyhow here is the thing I got myself a string, just a word. I need to locate a char in it and then erase it, also it must be done in a loop so that if char is in there twice etc. it would be deleted. Problem: What if char doesn't exist in the string. I tried to use if( !(blahblah.find(char)) ) doesn't work because if .find doesn't see the char it throws the value of char position out of range of length of string or drops it bellow zero, causes an out of range error. (This took me half a day to figure out) :mad: So I used: if( (blahblah.find(char) < 0) || (blahblah.find(char) => Length) ) same crap! Two hours later found strpbrk()! How can I use it together with strlen to simplify the condition and avoid out of range error during run-time. Basicaly what I need is to look at a string, single word. Check if it has a char I provide. If it doesn't I need the if-statment to fall thru. But if it does have it I need to erase it, this I can do :laugh:.

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        CreepingFeature wrote: ...I got myself a string... Would that be an STL string or an MFC CString?


        "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

        1 Reply Last reply
        0
        • N Nigel Savidge

          if (strchr (blahblah, char)) eraseit ; Is this what you are looking for?

          C Offline
          C Offline
          CreepingFeature
          wrote on last edited by
          #4

          Thanks!!! ;)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups