Thread: Need help with strings

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    6

    Need help with strings

    EspoirBondo:
    Need Help

    Creating a C program that can pick words in a string or array and save it in different location, later combine everything in one string or array.

    It will be using simple programming C code. For example (arrays, pointer) but not interrupts or other string functions.

    Hint:

    In a sentence like this:

    Size= 70

    $--GSV,x,x,x,x,x,x,x,...*hh
    $--GGA,hhmmss.ss,llll.ll,a,yyyyy.yy,b,x,xx,x.x,x.x,M,x.x,M,x.x,xxxx*hh
    $--GLC,xxxx,x.x,a,x.x,a,x.x,a.x,x,a,x.x,a,x.x,a*hh
    $--GSA,a,a,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x.x,x.x,x.x*hh

    The program must capture the $--GGA string then extract from this string:
    1) hhmmss.ss
    2) IIII.II
    3) a (if a = to N or S replace N or S with + or - respectively )
    4) yyyyy.yy
    5) b (if b = to E or W replace E or W with + or - respectively )

    Save them in an array and display as well.

    After all the saving, the program should combine them to one string.
    That the final output.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    Input a line using fgets() with a big buffer. Throw out any lines that are too long and discard the rest of the line.
    Look for the string "$--CGA" at the beginning of the line, if you don't find it, discard the line and read the next one.

    Check that the line is properly formatted, you have comas and decimal points in the right places, the right number of digits.
    If the fields are all fixed width, you can break them out easily. If they are variable width, you need to run a pointer along the line and keep track of where the field you are parsing ends and the next one begins.
    Check that the fields are valid.
    Do the processing of fields. In your case it's just replacing a and b with +/-.
    Use sprintf() or some other function to paste the fields back into your desired format.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You'll get nothing from me, (or many other people too I suspect), until you show your coding attempt at this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    6
    Thank you very much Malcolm Mclean I appreciate, may you help me by providing the code of this problem of mine please, because I tried is to difficult for me to capture the string $--GGA.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    6
    I don't know what to say, the scenario I posted is exactly what I have in my mind please if may help to solve this problem I will appreciate, I'm not good in programming.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please refer to the homework guidelines.

    There was a point in time when I was at bad in programming as you. If everyone just provided the code to solve the problems I faced with no effort on my part, I would still be as bad in programming as you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by EspoirBondo View Post
    Hint:

    In a sentence like this:

    Size= 70

    $--GSV,x,x,x,x,x,x,x,...*hh
    $--GGA,hhmmss.ss,llll.ll,a,yyyyy.yy,b,x,xx,x.x,x.x,M,x.x,M,x.x,xxxx*hh
    $--GLC,xxxx,x.x,a,x.x,a,x.x,a.x,x,a,x.x,a,x.x,a*hh
    $--GSA,a,a,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x.x,x.x,x.x*hh
    You probably think that you have described your problem well. But you have not.

    So I am going to ask several questions. You should be able to answer them for yourself. Hopefully, this will lead you to 1) restate the problem better and 2) be able to write some code yourself so you show enough effort to meet the homework policy.

    Where did the lines that you show above come from?

    Do these lines already exist in a text file? If so, what is the name of the file?

    Was the colorization just to help with explanation? Or is it in the input? If so, in what format? HTML commands or ANSI display terminal encoding or some other format.

    Did a program(s) generate the lines you have shown above?

    If these are a sequence of commands that was typed into the operating system command prompt (or shell), what is the CGA command?

    Or was all this the output of one command?

    Does your operating system have the ability to redirect standard output from a program to a file? Windows command prompt and Linux shells have this ability.

    What does size = 70 mean? Is it part of the input?
    Last edited by pheininger; 04-30-2013 at 04:35 AM. Reason: correct grammar and add and update questions

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Looks like the data format is discussed by the eminent Eric S. Raymond here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-16-2012, 06:08 AM
  2. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  3. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 10:26 PM
  4. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM
  5. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM