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:

char name[5] = “Lucy”;

We use the %s format specifier for strings:

printf(“%s will lead the parade.”, name);

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:

char city[ ];

To store the viewer’s input in the variable, add a space before %s:

scanf(” %s”, &city);

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:

char explorer[3][40] = {
{“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:

printf(“%s reached the South Pole first.”, explorer[1]);

Let’s put it all together:

#include<stdio.h>
void main( )
{

char explorer[3][40] = {
{“Ernest Shackleton”},
{“Roald Amundsen”},
{“Robert Falcon Scott”}
};

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.

Published by Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Join the Conversation

3 Comments

Leave a comment

Your email address will not be published. Required fields are marked *