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

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …