Declaring (Initializing) Strings
As we learned in the lesson on Characters and Strings, a string is a character variable with more than one character. Strings can be abbreviations, words, phrases, sentences, or even paragraphs.
To declare a string, use the char variable type, then specify how many characters are in the string. Add one to this number, because at the end of the string there is \0, which tells the computer that the string has ended. Here is our declaration:
We use the %s format specifier for strings:
Here is a simple program that declares and uses strings:
#include<stdio.h> void main( ) { char girl[5] = “Lucy”; char boy[7] = “Thomas”; printf(“If our baby is a girl, we will name her %s.\n”, girl); printf(“If we have a boy, his name will be %s.\n”, boy); } |
Type this source code in your editor and save it as baby.c then compile it, link it, and run it.
Viewer Input with Strings
If we want the viewer to type a string, then we must declare the character variable as empty:
To store the viewer’s input in the variable, add a space before %s:
Now put it all together:
#include<stdio.h> void main( ) { char city[ ]; printf(“What city do you live in? “); scanf(” %s”, &city); printf(“You live in %s.”, city); } |
Type this source code in your editor and save it as city.c then compile it, link it, and run it.
Functions: gets and puts
Did you have any problems with the previous example? For instance, if you typed “New York”, did the computer output only “New”? If this occurred, then you need to use a different function, instead of scanf.
Scanf works very well when the viewer’s input has NO spaces. However, inputs like “New York” or “Kuala Lumpur” have spaces. Scanf doesn’t handle these spaces very well. It thinks you’ve finished your input and cuts off the rest of the city name.
To solve this problem, we use the function gets. This function waits until the viewer hits the Enter key before it cuts off the input. This is great if the input has spaces, like “South America”.
To insure the proper output with gets, use its complimentary function, puts. This prints the viewer’s input on the screen. Here is an example:
#include<stdio.h> void main( ) { char name[ ]; printf(“What is your first and last name?”); gets(name); puts(name); } |
Type this source code in your editor and save it as myname.c then compile it, link it, and run it.
String Arrays
As we learned in the lessons on Arrays, an array is a set of elements, each with an index number starting at [0].
To declare a string array, give the number of strings, then the maximum number of characters(plus one for \0) in the longest string. Here is our declaration:
{“Ernest Shackleton”}, {“Roald Amundsen”}, {“Robert Falcon Scott”}
In the printf statement, we use the %s format specifier for strings, and give the index number for the string we want to use:
Let’s put it all together:
#include<stdio.h> void main( ) {
printf(“%s reached the South Pole first.”, explorer[1]); } |
Type this source code in your editor and save it as expl.c then compile it, link it, and run it.
nice bitt enhance it
mean explain how we use gets() and puts() with 2-d char array like name[3][30];