write a c program to scan a string and print on it its screen

20 Sep 2022 Balmiki Mandal 0 C Programming

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:

  1. Declare a character array to store the string.
  2. Use the scanf() function to scan the string from the console.
  3. Use the printf() function to print the string to the console.

C program to scan a string and print it on the screen:

C program
#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:

C program
#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:

 For further information and examples, Please visit[ C-Programming From Scratch to Advanced 2023-2024]

 

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!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.