Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

Program To Print Characters In A String in C

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

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

  1. 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).
  2. 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.
  3. 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".
  4. 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. 
  5. 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.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Key Takeaways

In this article, we have extensively discussed how to print characters in a string in C language and how to print the strings stored in the form of the character array. We use multiple examples to understand the concept of how we can print characters in a string in C language.

Visit here to know more about strings in the C language.

Check out this article - C++ String Concatenation  Bubble Sort Program in C

We hope that this blog has helped you enhance your knowledge on how to print characters in a string in C language, and if you would like to learn more, check out our articles in the code studio library. Do upvote our blog to help other ninjas grow. Happy Coding!

Topics covered
1.
Introduction
1.1.
Strings
1.2.
Format Specifier
1.3.
Examples
2.
FAQs
3.
Key Takeaways