Programming in C: Unit II (b): Strings

Introduction to Strings in C

Syntax with Example C Programs

Nowadays, computers are widely used for word processing applications such as creating, inserting, updating, and modifying textual data. Besides this we need to search for a particular pattern within a text, delete it, or replace it with another pattern.

Unit II : Arrays and Strings

CHAPTER 6 : STRINGS

Takeaways

• Reading and writing strings

• String and character functions

• Suppressing input

• Arrays of strings

• Operations on strings


INTRODUCTION TO STRINGS

Nowadays, computers are widely used for word processing applications such as creating, inserting, updating, and modifying textual data. Besides this we need to search for a particular pattern within a text, delete it, or replace it with another pattern. So there is actually a lot we as users do to manipulate the textual data.

Programming Tip: Character constants are enclosed in single quotes. String constants are enclosed in double quotes.

In C language, a string is a null-terminated character array. This means that after the last character, a null character ('\0') is stored to signify the end of the character array. For example, if we write

char str[] = "HELLO";

We are declaring a character array that has five usable characters namely, H, E, L, L, and O. Apart from these characters, a null character ('\0') is stored at the end of the string. So, the internal representation of the string becomes HELLO'\0'. To store a string of length 5, we need 5 + 1 locations (1 extra for the null character). The name of the character array (or the string) is a pointer to the beginning of the string. Figure 6.1 shows the difference between character storage and string storage.

If we declare str as,

char str[5] = "HELLO";

Then the null character will not be appended automatically to the character array. This is because str can hold only 5 characters and the characters in HELLO have already filled the locations allocated to it.

Note

When the compiler assigns a character string to a character array, it automatically appends a null character to the end of the string. Therefore, the size of the string should be equal to maximum number of characters in the string plus one.

Programming Tip: When allocating memory space for a character array, reserve space to hold the null character also.

Like we use subscripts (also known as index) to access the elements of an array, similarly subscripts are also used to access the elements of the character array. The subscript starts with a zero (0). All the characters of a string array are stored in successive memory locations. Figure 6.2 shows how str[] is stored in ed in memory.

Thus we see that a string is a sequence of characters. In Figure 6.2, 1000, 1001, 1002, and so on are the memory addresses of individual characters. From the figure, we see that H is stored at memory location 1000 but in reality the ASCII codes of characters are stored in memory and not the character itself, i.e., at address 1000, 72 will be stored since the ASCII code for H is 72.

char str[] = "HELLO";

The above statement declares a constant string as we have assigned value to it while declaring the string. However, the general form of declaring a string is

char str[size];

When we declare the string in this way, we can store size -1 characters in the array because the last character would be the null character. For example, char mesg [100]; can store a maximum of 99 usable characters.

Till now we have seen one way of initializing strings. The other way to initialize a string is to initialize it as an array of characters, like

char str[] = {'H', 'E', 'L', 'L', 'O', '\0'};

In this example, we have explicitly added the null character. Also observe that we have not mentioned the size of the string (or the character array). Here, the compiler will automatically calculate the size based on the number of elements initialized. So, in this example 6 memory slots will be reserved to store the string variable, str.

We can also declare a string with size much larger than the number of elements that are initialized. For example, consider the statement below:

char str [10] = "HELLO";

In such cases, the compiler creates a character array of size 10; stores the value "HELLO" in it and finally terminates the value with a null character. Rest of the elements in the array are automatically initialized to NULL. Figure 6.3 shows the memory representation of such a string.

Consider the following declaration:

char str[3];

str = "HELLO";

The above declaration is illegal in C and would generate a compile time error because of two reasons. First, the array is initialized with more elements than it can store. Second, initialization cannot be separated from declaration.

Note

An array name cannot be used as the left operand of an assignment operator. Therefore, the following statement is illegal in C.

char str2, str1[]="HI";

str2 = strl;

Reading Strings

If we declare a string by writing

char str[100];

Then str can be read by using three ways:

1. using scanf function

2. using gets () function

3. using getchar(), getch() or getche () function repeatedly

Strings can be read using scanf() by writing

scanf("%s", str);

Programming Tip: Using & operand with a string variable in the scanf statement is optional as string variable is a character array and denotes the address where the array begins.

Although the syntax of scanf() function is well known and easy to use, the main pitfall with this function is that the function terminates as soon as it finds a blank space. For example, if the user enters Hello World, then str will contain only Hello. This is because the moment a blank space is encountered, the string is terminated by the scanf() function. You may also specify a field width to indicate the maximum number of characters that can be read in. Remember that extra characters are left unconsumed in the input buffer.

Unlike int (%d), float (f), and char (c), %s format does not require ampersand before the variable name.

Note

When scanf() encounters a white space character, it ter- minates reading further and appends a null character to the string that has been read. The white space character is left in the input stream and might be mistakenly read by the next scanf() statement. So in order to delete the white space character from the input stream, either use a space in the format string before the next conversion code or FLUSH the input stream by using the fflush function by writing fflush(stdin).

The next method of reading a string is by using gets () function. The string can be read by writing

gets (str);

gets () is a simple function that overcomes the drawbacks of the scanf() function. The gets () function takes the starting address of the string which will hold the input. The string inputted using gets () is automatically terminated with a null character.

Last but not the least, strings can also be read by calling the getchar() function repeatedly to read a sequence of single characters (unless a terminating character is entered) and simultaneously storing it in a character array as shown below.

i=0;

ch = getchar(); // Get a character

while (ch != '*')

{

str[i] = ch;

// Store the read character in str

i++;

ch= getchar(); // Get another character

}

str[i] = '\0'; // terminate str with null character

Programming Tip: A compile time error will be generated if a string is assigned to a character variable.

Note that in this method, you have to deliberately append the charac- ters with a null character. The other two functions automatically do this.

Writing Strings

Strings can be displayed on screen using three ways:

1. using printf() function

2. using puts() function

3. using putchar () function repeatedly

A string can be displayed using printf () by writing

printf("%s", str);

We use the conversion character 's' to output a string. We may also use width and precision specifications along with %s (as discussed in Chapter 2). The width specifies the minimum output field width. If the string is short, extra space is either left padded or right padded. A negative width left pads short string rather than the default right justification. The precision specifies the maximum number of characters to be displayed. If the string is long, the extra characters are truncated. For example,

printf("%5.3s", str);

The above statement would print only the first three characters in a total field of five characters. Also these three characters are right justified in the allocated width. To make the string left justified, we must use a minus sign. For example,

printf("%-5.3s", str);

Note

When the field width is less than the length of the string, the entire string will be printed. Also if the number of characters to be printed is specified as zero, then nothing is printed on the screen.

The next method of writing a string is by using puts () function. The string can be displayed by writing

puts (str);

puts () is a simple function that overcomes the drawbacks of the printf() function. The puts () function writes a line of output on the screen. It terminates the line with a newline character ('\n'). It returns an EOF (-1) if an error occurs and returns a positive number on success. Last but not the least, strings can also be written by calling the putchar() function repeatedly to print a sequence of single characters.

i=0;

while (str[i] != '\0')

{

putchar (str[i]);

}

// Print the character on the screen

i++;

}

Note

Note one interesting point from the given fragment.

char str = "Hello";

printf("\n %s", str); // prints Hello

printf("\n %s", &str); // prints Hello

printf("\n %s", &str[2]); // prints llo

This is possible because a string is an array of characters.

Summary of Functions Used to Read and Write Characters

Table 6.1 contains a list of functions that are used to read characters from the keyboard and write characters to the screen.

1. Write a program to display a string using printf().

#include <stdio.h>

#include <conio.h>

int main()

{

char str[] = "Introduction to C";

clrscr();

printf("\n |%\s", str);

printf("\n |%20s|", str);

printf("\n |%-20s", str);

printf("\n %.4s", str);

printf("\n |%20.4s", str);

printf("\n %-20.4s", str);

getch();

return 0;

}

Output

| Introduction to C |

|         Introduction to C  |

| Introduction to C                   |

| Intr |

|         Intr |

| Intr         |

The printf() function in UNIX supports specification of variable field width or precision, i.e., if we write,

printf("\n %*.s*, w, p, str);

Then the printf() statement will print first p characters of str in the field width of w.

2. Write a program to print the following pattern.

H

H E

H E L

H E L L

H E L L O

H E L L O

H E L L

H E L

H E

H

#include <stdio.h>

#include <conio.h>

int main()

{

int i, w, p;

{

char str[] = "HELLO";

printf("\n");

for (i=0;i<5; i++)

{

p = i+1;

printf("\n %-5.*s", p, str);

}

printf("\n");

for (i=4;i>=0; i--)

{

p = i+1;

printf("\n %-5.*s", p, str);

}

getch();

return 0;

}

sprintf() Function

The library function sprintf() is similar to printf (). The only difference is that the formatted output is written to a memory area rather than directly to a standard output (screen). sprintf() is useful in situations when formatted strings in memory have to be transmitted over a communication channel or to a special device. The syntax of sprintf() can be given as

int sprintf( char * buffer, const char * format [ , argument, ..] );

Here, buffer is the place where string needs to be stored. The arguments command is an ellipsis so you can put as many types of arguments as you want. Finally, format is the string that contains the text to be printed. The string may contain format tags.

#include <stdio.h>

main()

{

char buf[100];

int num = 10;

sprintf(buf, "num = %3d", num);

}

Programming in C: Unit II (b): Strings : Tag: : Syntax with Example C Programs - Introduction to Strings in C