Locating a character

This is a discussion on Locating a character within the C++ Programming forums, part of the General Programming Boards category; How do I locate a specific character within a string? I know that there is probably a simple answer but ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    1

    Question Locating a character

    How do I locate a specific character within a string? I know that there is probably a simple answer but I am a student who is just learning.

  2. #2
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    I'd implement it in a function, it's much more elegant, and has the advantage of returning every occurence of the char or substring.

    Scan the string from it's first index until you find your character, then return it's index (you can even store it, then continue the search for another occurence).
    If you reached the end, and no char found, the function should return -1.
    Last edited by Carlos; 10-20-2001 at 02:03 AM.

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    The strstr() function will return the first occurance of a character or substring within another string.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I use strchr() for this....
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    4

    Question

    Is there any chance of an example of how to use this function?

    I am only a newbie to all this as well

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    char test[] = "hello world";
    char *p = strchr( test, 'w' );
    // p now points at the 'w' in world
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. syntax error when defining vectors
    By starkhorn in forum C++ Programming
    Replies: 5
    Last Post: 09-22-2004, 12:46 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

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