Instructions on using strings in C



Instructions on parsing time strings from sockets in C

(addressing string manipulations for CNT 4104 Assignment)

Janusz Zalewski

November 3, 2013

1. Strings in C are just arrays of characters, so there is no essential difference between them and arrays of integers, for example. The only difference is that of a type of elements (char versus int). One must remember, however, that the last element of a string (character array) is a null character (ASCII code ‘\0’) used to terminate the string – and this is a non-noticeable but fundamental difference.

2. Strings are therefore declared as arrays of characters as follows (example taken from the Assignment #3 template program – timeClient.c):

char buf[5000];

This statement declares an array of characters (a string) 5000 elements long.

Note. Following what was said in (1) above, one must remember that this string can

hold 4999 characters plus a null character terminating it.

3. Knowing (from the above) how strings are organized in C, one can easily parse time data from a socket, since they are delivered as strings. In particular, one can read a string character by character and print it. For example, when timeClient.c delivers time in the following format:

Tue Oct 4 11:59:43 2005

parsing it can be done as follows:

printf("Weekday: %c%c%c\n", buf[0], buf[1], buf[2]);

printf("%c%c hours\n", buf[11], buf[12]);

printf("%c%c minutes\n", buf[14], buf[15]);

printf("%c%c seconds\n", buf[17], buf[18]);

where

- buf[i] corresponds to successive characters of the string, and

- %c means formatting in C the printout for characters.

4. Knowing all that, you can now convert any substring composed of digits into an integer. For this to happen, do the following:

- first, declare the arrays to hold all relevant substrings, for example:

char hrs[3], min[3], sec[3];

(remember that, although hours, minutes and seconds are represented by 2 characters, you must have additional element for string termination, therefore arrays are 3 elements long)

- then assign them values from the string returned by a socket and stored in buf:

hrs[0] = buf[11];

hrs[1] = buf[12];

min[0] = buf[14];

min[1] = buf[15];

sec[0] = buf[17];

sec[1] = buf[18];

5. Having respective string values stored in hrs, min, and sec arrays, one has to know that a function to convert them to integers is called:

int atoi(char *)

which takes a string argument and returns an integer.

6. Finally, knowing all this, you can use this knowledge for a certain purpose, such as printing how many seconds elapsed since midnight (in the time delivered by the socket):

printf("%d seconds\n", \

atoi(hrs)*3600 + atoi(min)*60 + atoi(sec));

Note. This logic can be now used to parse the time from the host 172.22.33.129 (which I effectively did above), as well as time from time-a. and from 131.156.3.21 (which will remain open to us only till the end of this week) and from any other host (because you now know how to parse strings).

***

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download