getting chars from a string

This is a discussion on getting chars from a string within the C Programming forums, part of the General Programming Boards category; hello, im normally used to c++, but i now am trying to complete some basic tasks using 'c', i apologise ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    99

    getting chars from a string

    hello, im normally used to c++, but i now am trying to complete some basic tasks using 'c', i apologise now im not very good!.

    I have a string and i want to see if a specific criteria appears in it

    for example look for '45' in the string ' 45 dogs'

    I have herd of something called "str begin" but i have no idea how to work it and cant really find any help on the net, can anyone offer any advice?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not sure exactly what you are asking for. To get at individual characters, you can use str[n] where n is the position you want. You can also write a function that copies part of a string into another string, using a loop and the str[n] access.

    I'm pretty sure a basic tutorial of strings, like the one in the tutorial section of cprogramming.com, would be able to tell you more than I can in a shortish post.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    i see, you could be right however i was advised towards str-begin using syntax like

    Code:
    string mystring = "45 dogs in a house";
    strbegin ("45", mystring);
    but for some reason it doesnt like strbegin. I believe it returns a positive int if a match occurs, however i havent got that far yet

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm pretty sure strbegin isn't a standard function. It would not be hard to write one (in fact, strncmp() does almost the same thing, you just need to apply strlen on the first string).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Have a look at strstr(), this is probably what you are looking for.

    I know I should stop ending sentences with prepositions, but that's something I will not put up with.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

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