In this tutorial, we will discuss the concept of C Program to print Characters in a string
In this topic, we are going to learn how to display characters of a string in C programming language using for, while and do-while loops.
In this program, we are briefing how to display characters of a string using for loop in C language
Program 1
#include <stdio.h> #include <stdlib.h> int main() { char str[100]; int i; printf("Please enter a string!\n"); scanf("%s",str); printf("Characters of the given Strings!\n"); for(i=0; str[i]!= '\0'; i++){ printf("The charecters of str[%d] index= %c \n",i,str[i]); } getch(); return 0; }
When the above code is executed, it produces the following result
Please enter a string! Program Characters of the given strings The characters of str[0] index = P The characters of str[1] index = r The characters of str[2] index = o The characters of str[3] index = g The characters of str[4] index = r The characters of str[5] index = a The characters of str[6] index = m
In this program, we are briefing how to display characters of a string using while loop in C language
Program 2
#include <stdio.h> #include <stdlib.h> int main() { char str[100]; int i; printf("Please enter a string!\n"); scanf("%s",str); printf("Characters of the given Strings!\n"); i=0; while( str[i]!= '\0'){ printf("The charecters of str[%d] index= %c \n",i,str[i]); i++; } getch(); return 0; }
When the above code is executed, it produces the following result
Please enter a string! Program Characters of the given strings The characters of str[0] index = C The characters of str[1] index = o The characters of str[2] index = d The characters of str[3] index = i The characters of str[4] index = n The characters of str[5] index = g
In this program, we are briefing how to display characters of a string using do-while loop in C language
Program 3
#include <stdio.h> #include <stdlib.h> int main() { char str[100]; int i; printf("Please enter a string!\n"); scanf("%s",str); printf("Characters of the given Strings!\n"); i=0; do{ printf("The charecters of str[%d] index= %c \n",i,str[i]); i++; }while( str[i]!= '\0'); getch(); return 0; }
When the above code is executed, it produces the following result
Please enter a string! print Characters of the given strings The characters of str[0] index = p The characters of str[1] index = r The characters of str[2] index = i The characters of str[3] index = n The characters of str[4] index = t
Similar post
Java program to print integer in Java language
Java program to print string array
Java program to read and print string array
C program to print string array
C program to read and print string array
C++ program print string array
C++ program to read print string array
Suggested for you
10 simple ways to add two numbers in Java In this article, we will discuss…
Write a Python program to find the first n prime numbers In this article we…
Python: Calculate Average of odd and even in a list using loops In this post,…
Python: Average of Odd & Even Numbers from User Input In this post, we will…
Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…
Python program to calculate the sum of odd and even numbers in a list In…