Strings in C4 min read

This tutorial contains about the String in C programming. You will learn about the declaration, initialization of String, and the use of string with examples.

What is String?

A string is defined as a series of characters or an array of characters(terminated by a null character ‘\0’). Each character present in the array occupies one byte of memory. The manipulation of text(words or sentences) in a c program is done by String.

The termination character that is the ‘\0’ character is essential to add in the string as it is the only way to specify where the particular string ends.

Declaring and Initializing a String in C

You can declare the string in two ways:

1. By character array: Here we declare the character array with a single character as one element within the single quote. Since the last element of the array needs to be a null character so the size of the array is one more than the number of characters used. Hence always take one size more than you actually need in an array for the null character.

Memory presentation of the above character array in C/C++:

string

2. By String Literal: In this case, we can simply write the complete string(words or sentence) at once that is by the string literal. Here, you don’t have to add a null character at the end, the compiler will append ‘\0’ at the end of the string.

In both cases, you can either give the array size or you can just leave it blank and it will take the size according to the number of characters you will initialize it with.

For character %s format specifier is used in a program.


Let us see a simple example:

String program:

Output:

This Website: SimpleToCode
This Website Tutorial: Simple2code Tutorial


How to Read String from the User.

Taking input from the user is done by a built-in library function scanf(). Scanf() keeps on taking the input from the user until it encounters whitespace (space, newline, tab, etc.).

NOTE that we do not use the & operator(use for address) in scanf() function, since it is an array of characters and the name (str) itself indicates the base address of the array (string).

String C Program:

Output:

Enter Stream: Computer Science
Stream is: Computer

In the above program, only the Computer is displayed when we try to print the string that is because of the white space in between. Now this could be overcome by changing a line of code that is you need to write scanf("%[^\n]s", str) instead of scanf("%s", str). It will allow you to store the string until a newline(\n) is encountered.

Let see it in a program.

Output:

Enter Stream: Computer Science.
Stream is: Computer Science.


Passing String to a Function

Passing a string to a function is done in the same way as passing an array to a function.

Example: C program to pass a string to a function

Output:

Enter Stream: Computer Science.
Stream is: Computer Science.


String with Pointers

We have already seen the use of pointers with arrays. In the same way, we can use a pointer with a string in C to point the String. We can manipulate the elements of a string with the pointer.

Example 1: String and Pointer

Output:

String present in str: Simple2Code


Example 2: String and Pointer

Output:

Oth position: S
3th position: p
Oth position: S
7th position: C

As you can see we can manipulate the element with the help of the pointer too. We can increment or decrement the pointer in order to get to the required positioned element.


MORE

Find a pair with the Given Sum in an Array in Java

In this tutorial, we will write a java program to find the sum pair in an array. Before that, you may go through the following …
Read More

C++ Program to display Pascal Triangle

In this tutorial, we will write a C++ program to display pascal’s triangle. Before that, you may go through the following topic in C++. for …
Read More

C++ Program to Calculate Sum of Geometric Progression

In this tutorial, we will learn and write a program to find the sum of GP series in C++. Before that, you may go through …
Read More

C++ Program to find the Transpose of a Matrix

In this tutorial, we will learn and write a program to find the transpose of matric in C++. Before that, you may go through the …
Read More

How to Split a String in C++

Let us learn how to split string in C++ programming language. We will look at a few ways to split a string. The splitting of …
Read More

C++ Program to calculate Average of Numbers using Arrays

In this tutorial, we will learn and write a C++ program to find the average of numbers using an array. To understand the coding, you …
Read More