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