Write a c program to print character by character in given string

20 Sep 2022 Balmiki Mandal 0 C Programming

Character-by-Character Printing in C: String Manipulation

To write a C program to print character by character in a given string, we can use the following steps:

  1. Declare a string variable and initialize it to the string that we want to print.
  2. Create a loop that iterates over the string variable, one character at a time.
  3. Inside the loop, print the current character to the console.
  4. Exit the loop when we reach the end of the string variable.

C program to print character by character in a given string:

C programming
#include <stdio.h>

int main() {
  char str[] = "Hello, world!";

  int i;
  for (i = 0; str[i] != '\0'; i++) {
    printf("%c", str[i]);
  }

  printf("\n");

  return 0;
}

Output:

Hello, world!

This program works by first declaring a string variable called str and initializing it to the string "Hello, world!". The program then creates a loop that iterates over the str variable, one character at a time. Inside the loop, the program prints the current character to the console and then increments the loop counter. The loop exits when we reach the end of the str variable, which is indicated by the null character (\0).

We can also use the while() loop to print character by character in a given string. 

C program that uses the while() loop to print character by character in a given string:

C programming
#include <stdio.h>

int main() {
  char str[] = "Hello, world!";

  int i = 0;
  while (str[i] != '\0') {
    printf("%c", str[i]);
    i++;
  }

  printf("\n");

  return 0;
}

This program works in the same way as the previous program, but it uses the while() loop instead of the for() loop.

We can also use a recursive function to print character by character in a given string.

Recursive C function that prints character by character in a given string:

C Programming
void print_string(char str[]) {
  if (str[0] == '\0') {
    return;
  }

  printf("%c", str[0]);
  print_string(&str[1]);
}

int main() {
  char str[] = "Hello, world!";

  print_string(str);

  printf("\n");

  return 0;
}

This function works by recursively calling itself until it reaches the end of the string. The function exits when it reaches the end of the string, which is indicated by the null character (\0).

 

Whichever method we choose, printing character by character in a given string is a simple task in C programming.

Top Resources

string in c programming

What is the difference between strings and character arrays?

program to concatenate two strings without using strcat() function.

Write a one-line code to copy the string into the destination.

 

Further Reading:

 For further information and examples, Please visit[ C-Programming From Scratch to Advanced 2023-2024]

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.