My Blog
Articles related to programming, computer science, technology and research.

22/04/2016 Categories: C Programming. No Comments on Strings in C

Strings

A string is a collection of characters which is treated as a single data item. A group of characters enclosed in double quotes is known as a string constant. Some of the examples of string constants are:

  1. “hai”
  2. “hello world”
  3. “My name is suresh”

In C programming, there is no predefined data type to declare and use strings. So, we use character arrays to declare strings in C programs. The common operations that can be performed on a string are:

  • Reading and writing strings
  • Combining strings together
  • Copying one string to another
  • Comparing strings for equality
  • Extracting a portion of the string (substring)

 

Declaring and Initializing Strings

Like variables, we should declare strings before using them in the program. Since we already known that strings are implemented as character arrays, the syntax for declaring a string is as shown below:

string-decl

The size refers to the number of characters in the string. When the compiler assigns a character string to a character array, it appends a ‘\0’ to the end of the array. So, the size of the character array should always be number of characters plus 1.

Like numeric arrays and variables, character arrays can also be initialized when they are declared. Some of the examples for initializing the string are as shown below:

strings-examples

If less number of characters are provided than the size of the string, the rest of the characters are initialized to ‘\0’. If we try to assign more characters then the size of the string, compiler gives an error.

Note: The string termination character ‘\0’, is used to terminate a string. In C, there is no data type provided available. We maintain strings using character arrays. A string is a variable length structure stored inside a fixed size array. The size of the array is often larger than the number of characters in the string. So, the compiler must have some means to detect the end of the string. For this purpose we use ‘\0’.

Note: If ‘\0’ is not provided then the compiler will treat the array as a normal character array. Only when we provide the ‘\0’ character, compiler treats it as a string.

 

Reading Strings

Using scanf function

Strings can be read from the terminal by using the familiar scanf function. The format specifier to read strings is %s. An example of reading a string using scanf function is shown below:

reading-string-ex

There is a downside of using scanf function for reading strings. The downside is, scanf cannot read strings with white spaces. For example, if we provide “New Delhi” as the string, scanf will read only “New” and the rest of the characters are neglected or not processed. So, to read “New Delhi”, we have to declare two character arrays and read “New” and “Delhi” separately.

 

Reading and printing a line of text

Reading Text (getchar and gets)

As we have learned that scanf function cannot be used for reading a line of text, we have to search for other alternatives for reading a line of text which has white spaces embedded in it. One alternative is to read the line of text character by character until the enter key (\n) is pressed. Here we will use a predefined function called getchar( ) which reads a single character from the terminal.

In the above program we are reading characters until a new line character (\n) is encountered. So, by using the above approach we can read a line of text. There is a more efficient way for reading a line of text with white spaces. We can use the gets function which is available in the stdio.h header file. The purpose of gets function is to read a line of text from the terminal/keyboard. The usage of gets function is as shown below:

gets-syntax

The arrayname in the above piece of code is the array in which we are going to store the string. By using the gets function the above program can be rewritten as shown below:

Printing Text (putchar and puts)

We can print strings in multiple ways. First is by using printf function. We can use the format specifier %s to print a string. Let us consider the following example:

print-string

The output of the above piece of code is “hai”. The second way is to print the string character by character. For this purpose we can use the format specifier %c for printing each character or we can use the predefined function putchar. The usage of this function is as shown below:

putchar-syntax

In the above code, ch is the character that we want to print on the screen. By using the putchar function we can print a line of text or string as shown below:

The last alternative for printing a string or a line of text is by using the predefined function puts. This function is available in the stdio.h header file. By using the puts function we can rewrite the above program as shown below:

 

Suryateja Pericherla

Suryateja Pericherla

Hello, I am Suryateja Pericherla working as an Asst. Professor in CSE department at Vishnu Institute of Technology. I write articles to share my knowledge and make people knowledgeable regarding certain topics.
Suryateja Pericherla

Latest posts by Suryateja Pericherla (see all)

Related Links:

Leave a Reply

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

Scroll Up