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

This is a discussion on How do I print just word from a string and not numbers within the C Programming forums, part of the General Programming Boards category; Lets a string contains "Hello 1 2 3 4 5" how do I print just Hello and not everything else. ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    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
    Posts
    2,553
    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
    Location
    Oregon, USA
    Posts
    3,163
    If you don't mind hacking up your original string, you can do:
    Code:
    *strchr(str, ' ') = '\0';
    printf("%s", str);
    Otherwise, maybe:
    Code:
    char *s;
    for(s = str;*s != ' ';++s)
      putchar(*s);
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    26
    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


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