Lab – 6 Character Arrays and Strings-use and manipulate strings and arrays of characters
60-141 – Introduction to Programming II
Objective: Learn 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, ‘\0’. 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.
1. Declare a char array called buffer1 and initialize it to “this is the first buffer.” using
this method: {‘t’, ‘h’, ‘i’, ‘s’, ‘ ‘, ‘i’, … ‘\0’};
2. Declare a char array called buffer2 and initialize it to “this is the second buffer.”
using this method: “this is the second buffer”;
3. Declare a char array called buffer3 of size 80 and leave it un-initialized.
4. 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.
5. Now, use printf to print all the three buffers. (Again, use %s as the formatting character for string data).
6. Declare a pointer variable named pBuffer and initialize it to point to buffer3. (Use char * pBuffer).
7. Display the contents of buffer3 using pBuffer; do not use subscripts.
8. 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.
1. 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 ‘\0’ symbol to keep it at the end of the array and watch out for odd and even length of strings.
2. Test your function and display the string (array) before and after it is reversed.
3. IMPORTANT: The function does NOT print the string in reverse; it actually reverses it in the memory.
Part C. String Tokenization
1. Write a function called ParseSentence that takes as input parameter a null (i.e. ‘\0’) terminated string S, where S would contain an English sentence.
2. Assume that the delimiters are space, comma, semicolon and period.
3. The function extracts each word from the sentence (without any punctuation or spacing) and then prints one word per line.
4. 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
Summary of the lab requirements: You must write a single C program called Lab6.c that contains declarations for various char type data structures, and also contains definitions for functions Reverse() and ParseSentence(), following the requirements and specifications outlined in Parts A, B and C above. Don’t forget to test your program with various inputs. Remember to fully document your programs.
Answer:
/* Title: Lab#6: Character Arrays and Strings Objective: This program will reverse a char array or a string, also it takes as input parameter a null(\0) terminated string and then prints one word per line. */ //C-Preprocessor Directives #include <stdio.h> #include <string.h> //Function Prototypes void Reverse(char *str); void Swap (char *x, char *y); void ParseSentence(char *str); int main (void){ //Declare and initialize a char array buffer1 char buffer1 [] = {'t','h','i','s',' ','i','s',' ','t','h','e',' ','f','i','r','s','t',' ','b','u','f','f','e','r','\0'}; //Declare and initialize a char array buffer2 char buffer2 [] = "this is the second buffer"; //Declare a char array buffer3 of size 80 and un-initialized char buffer3 [80]; char str[] = "Hello"; // Initialize buffer3 from the keyboard using the scanf printf("Please enter a string: "); scanf(" %[^\n]s ", buffer3); //Use printf to print all the three buffers printf("%s\n", buffer1); printf("%s\n", buffer2); printf("%s\n", buffer3); //Declare a pointer variable and initialize it to point to buffer3 char *pBuffer = buffer3; //Display the contents of buffer3 printf("%s\n", pBuffer); //Advance the pointer pBuffer to display the rest of buffer3 pBuffer += 4; printf("%s\n", pBuffer); printf("\nReversing \"%s\"\n", str); Reverse(str); printf("%s\n", str); char str2[] = "Hello world, how are you today."; printf("\nParsing sentence\n\n"); ParseSentence(str2); return 0; } /* Objective: This function reverse the contents of an array (or string) being passed. Input:An array or string Output: Return the reverse of a string */ void Reverse (char *stringArr){ int i; int size = strlen(stringArr); int mid; if (size%2 == 0) mid = size / 2 - 1; else mid = size/2; for ( i = 0 ; i < mid ; i++){ Swap (stringArr + i, stringArr + size - 1- i); } } /* Objective: This function swap the position of two chars. Input:Two char x and y Output: Return the two passed char swapped */ void Swap (char *x, char *y){ char temp = *y; *y = *x; *x = temp; } /* Objective: This function extracts each word from the sentence and the prints one word per line Input:A null (\0) terminated string Output: Return the sentence one word per line. */ void ParseSentence (char stringArr[]){ char *delim = strtok(stringArr," ,\n");; while (delim != NULL){ printf("%s\n", delim); delim = strtok(NULL," ,\n");; } }
Leave a reply