Array

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 display characters of a string in C programming language  using for, while and do-while loops.

Code to  display characters of a string in C

characters

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 <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


Code to display Characters in 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 <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

 

 

Code to display Characters in 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 <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

for loop in C language

while loop in C language

do-while loop in C language

Operator in C language

Data type in C language

variable in C language

 

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

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

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