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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago