Codeforcoding

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.

C++ Program to print Characters in a string
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

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
Exit mobile version