write a c program to scan a string and print on it its screen
C Program to Scan and Print a String
To write a C program to scan a string and print it on the screen, we can use the following steps:
- Declare a character array to store the string.
- Use the scanf() function to scan the string from the console.
- Use the printf() function to print the string to the console.
C program to scan a string and print it on the screen:
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
printf("The string you entered is: %s\n", str);
return 0;
}
This program will prompt the user for a string and then print the string to the console. For example, if the user enters the string "Hello, world!", the program will print the following output:
Enter a string: Hello, world!
The string you entered is: Hello, world!
We can also use a for loop to iterate over the characters of the string and print each character to the console.
C program to scan a string and print it on the screen using a for loop:
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
for (int i = 0; i < strlen(str); i++) {
printf("%c", str[i]);
}
printf("\n");
return 0;
}
This program is equivalent to the previous example, but it uses a for loop to iterate over the characters of the string and print each character to the console.
Strings are a powerful tool in C programming. They can be used to store and manipulate text data in a variety of ways.
Top Resources
Write a program to print a string with space in c
Difference Between Character array and string in c
Further Reading:
Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!