Thread: strings

  1. #1
    Unregistered
    Guest

    Question strings

    How can I extract a specific string from a string?

    The string comes over a serial port in the form of "@15:28:08D ". It just so happens that this string is the time of day with a character on the beginning and at the end.

    I need to extract the hours, minutes, and seconds only.

    I would certainly appreciate any and all help!

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788

    Lightbulb

    Without using library functions, you could just use this.

    char date[MAX];
    char temp[MAX];

    int i , j;

    for( i=1, j=0; date[i] != '\o'; i++, j++)
    {
    temp[j] = date[i];
    }

    temp[j-1] = '\o';

  3. #3
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    If it is always in the format of "@15:28:08D"

    Then maybe you could;

    char hour[2];
    hour[0] = date[1];
    hour[1] = date[2];

    int thehour = atoi(hour);

    I think that would work, I don't really know though. If it does you could do the same thing to get the minutes and the seconds.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    sscanf is like scanf, or fscanf, but it scans another string as the source.
    strstr will locate a given string in another string.

    These are just a few options.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Or you could do this.....
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	char mystring[] = "@15:28:08D";
    	
    	printf ("string is %s\n", mystring);
    	memmove(mystring, mystring+1, 8);
    	mystring[8] = '\0';
    	printf ("string is now %s\n", mystring);
    	return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > Or you could do this.....

    Well hell, if you're going to be that way about it!

    printf("%s", string + 1 );


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Unregistered
    Guest

    Question

    The problem is that the string I'm looking for (time of day, HH:MIN:SEC) is updating constantly and therefore is a dynamic string. How do I find a dynamic string on a line and then how can I get it into the variable I want it to go to?

    A second question I forgot to add previously. How can I find another string on a line that is not always at the same location in the line? This string isn't always the same, it can be one of 158 possibilities. It is always 5 or 6 characters long and always has an underscore as it's third character. I would greatly appreciate any help!
    Thanks!

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Unregistered
    The problem is that the string I'm looking for (time of day, HH:MIN:SEC) is updating constantly and therefore is a dynamic string. How do I find a dynamic string on a line and then how can I get it into the variable I want it to go to?

    A second question I forgot to add previously. How can I find another string on a line that is not always at the same location in the line? This string isn't always the same, it can be one of 158 possibilities. It is always 5 or 6 characters long and always has an underscore as it's third character. I would greatly appreciate any help!
    Thanks!
    Meaning what exactly? How is it a "dynamic string"? You must have some pointer to this string. If so, what's the problem? If not, then your question really is "how do I read from ___?"

    If you have a file stream, which is what you use to read from specific devices, then just use fread or something similar.

    For your second question, as I've already stated, use sscanf.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Unregistered
    Guest
    quzah,
    Maybe dynamic was an improper description. What I need to do is take this string "@HH:MM:SSD" and extract the HH, MM, and SS separately and put each respective one into a variable. Can you or somebody else show me how this can be done?
    Thanks!

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Did you even look at sscanf, because I really think that sscanf will do the trick. Once again, might I suggest sscanf? I find sscanf to be a handy tool for this sort of thing. Perhaps you should consider sscanf. sscanf is quite useful in this situation. This is one of those times when sscanf would help out.

    And finally...

    sscanf should do the job nicely.

    Get the message?

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Unregistered
    Guest
    sscannnnf. I tried it but it blows up!

  12. #12
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    Code:
    #include <string.h>
    #include <stdio.h>
    
    int main(){
        char string[] = "@15:28:08D";
        int hour, minute, second;
        sscanf(string,"%*c%d:%d:%d%*c",&hour,&minute,&second);
        printf("%d\n%d\n%d\n",hour, minute, second);
        return 0;
    }
    Enough said.

    kwigibo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM