Introduction
In this blog we will learn how to print characters in a string in C++ language. Whenever we want to store a sequence of characters, such as the name of students, etc., then we use a character array of some predefined size in any programming language, such as C or C++.
Strings
What are the strings?
Strings are the sequence of characters used to convey information in texts, or we can say that a string is a one-dimensional array of characters that must always end with the special character '\0' (NULL). Because each character occupies one byte of memory and because a string always ends with a null character, the size of the character array used to store the string is always one greater than the number of characters in the string/word to be stored.
To take a character as an input or to print a character, we use %c as format specifier in C language, and we use %s as format specifier in case of strings in C language.
You can also read about dynamic array in c and C Static Function.
Format Specifier
The format specifiers in the C language are used for input-output purposes. Using the concept of format specifier, the compiler can determine what sort of data is in a variable when using the scanf() and printf() functions to take input and print it.
Using some examples, let's understand how to print characters in a string in C language.
You can also read about Starcat () in C, and Tribonacci Series
Examples
1. C program to print characters in a simple string
Code:
#include <stdio.h>
int main()
{
// printf print the argument (a string) passed to it
printf("This is a simple string.");
return 0;
}
Output
This is a simple string. |
2. C program to print characters stored in a string as a character array
Code:
#include <stdio.h>
int main()
{
char arr[100] = "Hey, I am learning the C language through blogs of Coding Ninjas.";
printf("%s", arr);
// here %s is format specifier for string
return 0;
}
Output
Hey, I am learning the C language through blogs of Coding Ninjas. |
Also read reverse a number.
3. Program to print characters in a string in C
Code:
#include <stdio.h>
int main()
{
char str[100]="NINJA";
//Printing the characters with their index position of the given string str
for(int i = 0; str[i] != '\0'; i++)
{
printf("The Character at %d Index Position = %c \n", i, str[i]);
}
return 0;
}
Output
The Character at 0 Index Position = N The Character at 1 Index Position = I The Character at 2 Index Position = N The Character at 3 Index Position = J The Character at 4 Index Position = A |
The above for loop terminates with '\0' (NULL character), which marks the end of the string.
4. Program to print characters in a string in C using while loop
We can do the same thing as above by using a while loop in place of for loop.
Code:
#include <stdio.h>
int main()
{
char str[100]="NINJA";
int c=0;
printf("Individual characters from the given string:\n");
//Printing the characters of the given string str with spaces between them
while (str[c] != '\0') {
printf("%c ", str[c]);
c++;
}
return 0;
}
Output
Individual characters from the given string: N I N J A |
For practice, you can try this code on an online C compiler.
The programs above explain various methods to print characters in a string in C language. Now let's move on to the FAQ section.
Must Read what is storage class in c and Decision Making in C
FAQs
-
What are the strings in the C language?
Strings are the sequence of characters used to convey information in texts, or we can say that a string is a one-dimensional array of characters that must always end with the special character '0' (NULL). -
What happens if we don't have a null character at the end of the string?
A null char is required by several functions that work with strings. Because the pointer tries to access memory that is not owned by the program and in the absence of a null character, the program will terminate with a Segmentation Fault. -
Are there any built-in functions to manipulate strings in C language?
Yes, C language offers various in-built functions to manipulate strings, and all these functions are present in the C standard library " string. H". -
Why does format specifiers are used in the C language?
The format specifiers in the C language are used for input-output purposes. Using the concept of format specifier, the compiler can determine what sort of data is in a variable when using the scanf() and printf() functions to take input and print it. -
What are %c and %s, and why are they used?
The %c and %s are the format specifiers in C language, and to take a character as an input or to print a character, we use %c, and in the case of strings, we use %s as the format specifier.