Description
,.’rn to use and manipulate strings and arrays of characters.
In C, a string is simply defined as an array of characters terminated by the null character, or string delimiter, ”. If we want to store a string “hello”, which has only 5 characters, we need an array of minimum size 6. For example the array S below that has been declared as char S[6]. The array index (or subscript) values are indicated above each box containing a single character in the figure below.
We can also create a pointer to S: char *ptrS = S. Then, if we apply pointer arithmetic and increment the pointer ptrS++ we can show that the pointer ptrS now points to the second item in the array with index 1 (refer to the figure below). Now we can refer to the letter ‘e’ by either using the pointer as *ptrS, or using the array index as S[1].
Lab Work to do:
Write a complete C language program called Lab6.c. The requirements and specifications for the program are listed below in Parts A-C. Read the entire set of lab instructions carefully before proceeding in order to appreciate and understand how to approach the design of your program. Apply both Top-Down and Bottom-Up aspects of design.
Your program must treat each Part separately, but it is not required to have a menu, nor to loop back to receive multiple inputs – just do what is asked of you!
Part A. Character array and string.
- Declare a char array called buffer1 and initialize it to “this is the first buffer.” using
this method: {‘t’, ‘h’, ‘i’, ‘s’, ‘ ‘, ‘i’, … ”};
- Declare a char array called buffer2 and initialize it to “this is the second buffer.” using this method: “this is the second buffer”;
- Declare a char array called buffer3 of size 80 and leave it un-initialized.
- Use the scanf function to initialize buffer3 from the keyboard. (Note: use %s as the formatting character for string). If you encounter problems, document them and work out solutions for them.
- Now, use printf to print all the three buffers. (Again, use %s as the formatting character for string data).
- Declare a pointer variable named pBuffer and initialize it to point to buffer3. (Use char * pBuffer).
- Display the contents of buffer3 using pBuffer; do not use subscripts.
- Advance the pointer pBuffer a few positions and display the rest of buffer3 (i.e.
only from that position to the end of the string) using pBuffer.
Part B. String Manipulation: Reverse.
- Write a function called Reverse that accepts as a parameter a char array (or char pointer) and returns a void type. The function reverses the contents of the array (or string) being passed. Example: the string “Hello” will be reversed to “olleH”. You have to be careful about the ” symbol to keep it at the end of the array and watch out for odd and even length of strings.
- Test your function and display the string (array) before and after it is reversed.
- IMPORTANT: The function does NOT print the string in reverse; it actually reverses it in the memory.
Part C. String Tokenization
- Write a function called ParseSentence that takes as input parameter a null (i.e. ”) terminated string S, where S would contain an English sentence./
- Assume that the delimiters are space, comma, semicolon and period.
- The function extracts each word from the sentence (without any punctuation or spacing) and then prints one word per line.
- The function returns a void type.
For example: char str[] = “hello world, how are you today.”;
ParseSentence(str); would print the following: hello world how
are you
today


