Loop

C++ Program to print Characters in a string

C++ Program to print Characters in a string

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.

characters

Code to  display characters of a string in C++

Code to display characters of a string using for loop

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


Code to display Characters of a string using while loop

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

 

 

Code to display Characters of a string using do while loop

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

for loop in C++ language

while loop in C++ language

Do-while loop in C++ language

Data type in C++ language

Variable in C++ language

Operator in C++ language

 

 

Program to print Characters of a string in Java
C program to print Characters in a string
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago