C++ implements methods to copy input to output

  • 2020-04-02 03:12:31
  • OfStack

This article illustrates a C++ implementation of copying input to output. Share with you for your reference. The specific implementation method is as follows:

Copy the input to the output program and replace the tabs with \t, the backslash with \b, and the backslash with \\


#include <stdio.h>
main()
{
 int ch;
 ch=getchar();
 while(ch != EOF){
  if(ch == 't'){
   putchar('\');
   putchar('t');
  }
  else if(ch == 'b'){
   putchar('\');
   putchar('b');
  }
  else if(ch == '\'){
   putchar('\');
   putchar('\');
  }
  else
   putchar(ch);
 ch=getchar();
 }
}

A program that copies input to output and replaces successive Spaces with one:


#include <stdio.h>
main()
{
 int ch;
 int n=0;
 ch=getchar();
 while(ch != EOF){
  if(ch == ' '){
   n++;
   if(n<=1)
    putchar(ch);
  }
  else if(ch == 'n'){
   putchar(ch);
   n=0;
  }
  else{
   putchar(ch);
   n=0;
  }
  ch=getchar();
 }
}

Hope that the article described in the C++ programming to help you.


Related articles: