简体   繁体   中英

C program with return carriage \r

How do I write a C program that allows me to make carriage return for each white space in the Input?

Like for example:

Input: "I love programming"

Output:

I 
love
programming

What I did:

#include<stdio.h>
#include <ctype.h>

int main() {
    
    

int ch;
 char str[50];

while ((ch = getchar()) != EOF) {
  putchar(isspace(ch) ? '\r' : ch);
}
  scanf("%s", str);
  printf("%s", str);

  return 0;
}

Read a character. If a white space , print a '\r'

#include <ctype.h>

int ch;
while ((ch = getchar()) != EOF) {
  putchar(isspace(ch) ? '\r' : ch);
}

Or if you want a new line :

  putchar(isspace(ch) ? '\n' : ch);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM