Write a program to print a string with space in c

20 Sep 2022 Balmiki Mandal 0 C Programming

C Program to Print a String with Spaces

There are two main ways to print a string with spaces in C: using the printf() function or the puts() function.

  1.  Using the printf() function
  2.  Using Puts() function.
  3.  using %[^\n]s

1. Using the printf() function:

The printf() function is a more flexible way to print a string with spaces, as it allows you to specify the format of the output. To print a string with spaces using the printf() function, you can use the following format specifier:

C programming

"%s"
This format specifier tells the printf() function to print the string exactly as it is, including any spaces. For example, the following code will print the string "Hello, world!" with a space after the comma:

C programming

printf("Hello, %s!\n", "world");

2. Using the puts() function:

The puts() function is a simpler way to print a string with spaces, but it is less flexible than the printf() function. To print a string with spaces using the puts() function, you can simply call the function with the string as an argument. For example, the following code will print the string "Hello, world!" with a space after the comma:

C programming

puts("Hello, world!");

C program that prints a string with spaces using both the printf() and puts() functions:

C programming
#include <stdio.h>

int main() {
  char string[] = "Hello, world!";

  printf("%s\n", string);
  puts(string);

  return 0;
}

This program will print the following output:

Hello, world! 
Hello, world!

The puts() function will print the string "Hello, world!" to the console, followed by a newline character. The puts(" ") function will print a space to the console, followed by a newline character.

Both the printf() function and the puts() function can be used to print a string with space in C. The printf() function is more flexible, as it allows us to specify the format of the output. The puts() function is simpler to use, but it is less flexible.

 

Advance to must know  

3. Using %[^\n]s Function

The %[^\n]s format specifier is used with the scanf() function in C to read a string from user input until a newline character ('\n') is encountered. The % symbol indicates the start of a format specifier, [^\n] is a scanset that matches any character except a newline, and s indicates that the input should be interpreted as a string.

 

How to use the %[^\n]s format specifier to print a string with spaces:

#include<stdio.h>
#include<string.h>
int main() {
    char str[50];
    printf("Enter the any string with space:");
    scanf("%[^\n]s",str);
    printf("\nthe entered string is : %s",str);
}

This code will prompt the user to enter a string, read the string using scanf(), and print the entered string using printf(). The %[^\n]s format specifier will ensure that the entire string is read, including any spaces.

Top Resources

 

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.