Thread: How do I print just word from a string and not numbers

  1. #1
    Registered User
    Join Date
    Oct 2011

    How do I print just word from a string and not numbers

    Lets a string contains "Hello 1 2 3 4 5"

    how do I print just Hello and not everything else.
    Is there a method to print until the space.

    I know I can print the first 5 characters with a loop, but i want a different method to stop just at the space.

  2. #2
    Registered User
    Join Date
    May 2009
    The function strtok is a possible option; I have not used it in years. So, I will be no further help.
    strtok - C++ Reference

    Tim S.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Oregon, USA
    If you don't mind hacking up your original string, you can do:
    *strchr(str, ' ') = '\0';
    printf("%s", str);
    Otherwise, maybe:
    char *s;
    for(s = str;*s != ' ';++s)
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Nov 2011
    I thought the following might help from the string.h library. C string functions
    You would have to write a function to "slice" the non-numeric chars out of your string but you could use strpbrk to determine locations of the numeric values.

    If you coppied it one char at a time you could write a while loop with an exit condition of NULL and copy the non-numeric chars one at a time into your new array. You'd have to remember to manually add a NULL termination to the end of the new array afterwards.

    char *strpbrk( const char *s1, const char *s2)
    • returns a pointer to the first instance in s1 of any character found in s2. Returns a NULL pointer if no characters from s2 are encountered in s1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading text-and-numbers file word by word
    By bored_guy in forum C Programming
    Replies: 22
    Last Post: 10-26-2009, 10:59 PM
  2. how to print only N numbers of string
    By umen242 in forum C Programming
    Replies: 1
    Last Post: 06-26-2008, 12:55 AM
  3. Need help to print even numbers
    By ortegac in forum C Programming
    Replies: 3
    Last Post: 05-21-2006, 12:01 AM
  4. String of Word and Numbers
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-15-2002, 07:05 AM

Tags for this Thread