Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    16
    ReputationRep:
    Hi,

    I need to search a string for a specific character, and store the position of that character in the string as an integer NOT character.

    For example:

    String: 06/09/1992

    Say I want to get the position of the first, second, and third slash.

    So, let's store them into int variables slash1, slash2, & slash3

    So slash1 should = 2
    slash2 should = 5

    and so on.

    What is the best code I could use for this?

    Thanks in advance.
    Offline

    1
    ReputationRep:
    If you want to do it your self then you could go through the string and compare each character to a / and then store the array index.

    There may be a better function (probably as part of the string operations defined in string.h). If you're trying to separate out the parts in a date then something like strtok might also work.
    Offline

    0
    ReputationRep:
    Can you use a regex library? (edit: sorry, have re-read the q and that's probably overkill.)
    Offline

    16
    ReputationRep:
    As mfaxford says, the "proper" way of doing this, without the use of abstracted string manipulation functions, is simply to loop over the string, compare each character to '/' and return the loop index when you find a match.

    Bear in mind that a string is just a null-terminated ('\0') array of characters.
    • Thread Starter
    Offline

    16
    ReputationRep:
    (Original post by mfaxford)
    If you want to do it your self then you could go through the string and compare each character to a / and then store the array index.

    There may be a better function (probably as part of the string operations defined in string.h). If you're trying to separate out the parts in a date then something like strtok might also work.

    (Original post by tangledweb)
    Can you use a regex library? (edit: sorry, have re-read the q and that's probably overkill.)

    (Original post by Planto)
    As mfaxford says, the "proper" way of doing this, without the use of abstracted string manipulation functions, is simply to loop over the string, compare each character to '/' and return the loop index when you find a match.

    Bear in mind that a string is just a null-terminated ('\0') array of characters.
    Thanks for your help guys, I managed to come up with a way.

    C doesn't seem to have a very "manageable" way of approaching a problem like this unfortunately though.
    Offline

    2
    ReputationRep:
    (Original post by Sgt.Incontro)
    Thanks for your help guys, I managed to come up with a way.

    C doesn't seem to have a very "manageable" way of approaching a problem like this unfortunately though.
    There is no other way to do it in any language - they will all cycle through each character and compare each individually.

    if by manageable you mean someone has already written it out longhand and stored the method in a library for you to use in 2 seconds, man-up - it's simple enough code
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    What's your favourite Christmas sweets?
    Useful resources
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.