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 print 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 <iostream> #include <conio.h> using namespace std; int main() { char str[100]; int i; cout<<"Please enter a string!\n"; cin>>str; cout<<"Characters of the given Strings!\n"; for(i=0; str[i]!= '\0'; i++){ cout<<"The character of "<<" "<<i<<" position : "<<str[i] <<"\n"; } getch(); return 0; }
When the above code is executed, it produces the following result
Please enter a string! MyCode Characters of the given strings! The character of 0 position: M The character of 1 position: y The character of 2 position: C The character of 3 position: o The character of 4 position: d The character of 5 position: e
In this program, we are briefing how to display characters of a string using while loop in C++ language
Program 2
#include <iostream> #include <conio.h> using namespace std; int main() { char str[100]; int i; cout<<"Please enter a string!\n"; cin>>str; cout<<"Characters of the given Strings!\n"; i=0; while(str[i]!= '\0'){ cout<<"The charecters of "<<" "<<i<<" position : "<<str[i] <<"\n"; i++; } getch(); return 0; }
When the above code is executed, it produces the following result
Please enter a string! While Characters of the given strings! The character of 0 position: W The character of 1 position: h The character of 2 position: i The character of 3 position: l The character of 4 position: e
In this program, we are briefing how to display characters of a string using do-while loop in C++ language
Program 3
#include <iostream> #include <conio.h> using namespace std; int main() { char str[100]; int i; cout<<"Please enter a string!\n"; cin>>str; cout<<"Characters of the given Strings!\n"; i=0; do{ cout<<"The charecters of "<<" "<<i<<" position : "<<str[i] <<"\n"; i++; } while(str[i]!= '\0'); getch(); return 0; }
When the above code is executed, it produces the following result
Please enter a string! DoWhile Characters of the given strings! The character of 0 position: D The character of 1 position: o The character of 2 position: W The character of 3 position: h The character of 4 position: i The character of 5 position: l The character of 6 position: e
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
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…