Design a Function to print the string character by character in c

26 Sep 2022 Balmiki Mandal 0 C Programming

Printing Characters in a String Using a Custom C Function

To design a function to print a string character by character in C, we can use the following steps:

Declare a function with the following prototype:

C Programming
void print_string_char_by_char(char *str);

The function takes a pointer to a string as input and returns nothing.

Inside the function, we can use a for loop to iterate over the string and print each character:

C Programming
void print_string_char_by_char(char *str) {
  int i;
  for (i = 0; str[i] != '\0'; i++) {
    printf("%c", str[i]);
  }
}

The for loop iterates over the string until it reaches the null terminator character (\0). For each character, we use the printf() function to print it to the console.

Finally, we can return from the function:

C Programming
void print_string_char_by_char(char *str) {
  int i;
  for (i = 0; str[i] != '\0'; i++) {
    printf("%c", str[i]);
  }
  return;
}

C program that uses the print_string_char_by_char() function:

C Programming
#include <stdio.h>

void print_string_char_by_char(char *str);

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

  print_string_char_by_char(str);

  return 0;
}

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

Output:

 

H
e
l
l
o
,
 
w
o
r
l
d
!

Further Reading:

Concatenating Two Strings in C: A Step-by-Step Guide

Design a function to count given character in given string

Design a function to reverse the given string

Design a function to search given character in given string in c

Design a function to reverse the word in given string

Design a function replace the words in reverse order in a given string line

design a function to reverse data in between two location

Design a function to find string length in c

Design a Function to print the string character by character

Design a function to print the elements of a given integers array.

Design a function for bubble sort in c

Design a function to swapping of two number

Design a function to check the given number is Armstrong or not if Armstrong return 1 else 0

 

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

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.