[ad_1]
4.3 Guided PracticeCharacter Arrays
Character arrays are used to store strings. Each letter in the word is stored in one element of the array. A string array always ends with the null character ‘\0’. This is how we are able to tell the end of the string. This is useful when searching and manipulating strings.
There are multiple ways to store strings in character arrays. Let’s look at some coding examples.
char title[50] = “Chief Operating Officer”;
The example above is showing how we can store a line of text enclosed in double-quotes (just like you see in a printf() statement) upon declaration. Notice the data type is char for character and the size is 50.
char title[20]; = { ‘C’, ‘E’, ‘O’, ‘\0’};
The above example is showing how you can store the characters in a string letter by letter. Notice that each letter is enclosed with single quotes and the last character is the null character.
When storing strings into an array we can use the gets() function. When displaying strings in arrays we use the puts() functions. When using these functions you need to use the %s conversion specifier. If you want to use the %c (character conversion specifier) you will need to use a looping structure to step through each element of the array.
If you haven’t already, please read the following in your textbook, Deitel, P. &Deitel, H. (2015). C how to program, 8th ed., before beginning:
Instructions
Follow these instructions to complete your assignment:
- Select the Code tab and enter the code into your compiler.
- Compile your code and run. Your output should match the image on the Output tab.
- Paste a screenshot of your output (including the Title Bar showing the path and name of your code) into a Word document (.docx).
- Submit the “.c” code file, which should include a comment line with your name and the date.
- You will not create a flowchart with this exercise.
- Save your files and upload them using the instructions below.
[ad_2]