C++ method to remove white space in an input line

  • 2020-04-02 03:08:02
  • OfStack

This article illustrates a C++ method to remove white space from input lines. Share with you for your reference. The specific implementation method is as follows:



#include <stdio.h>
//No more than 1000 characters are stored in each line and no more than 1000 lines are entered
#define MAX 1000
int getline(char line[],int limit);
main()
{
 int nn;
 int i=0,j,k;
 char line[MAX];
 char saveline[MAX][MAX];
 while((nn = getline(line,MAX) ) > 0 ){
   if(i < MAX){
    for(j = 0;j < MAX;j++){
    
     saveline[i][j] = line[j];
    }
    i++;
   }
 }
 printf("The new line is:n");
 for(k = 0;k < i;k++){
  printf("%s",saveline[k]);
 }
}
int getline(char vline[],int vlimit)
{
 int ch;
 int i=0;
 int j=0;
 while(((ch = getchar()) != EOF) && (ch != 'n')){
  if(i < (vlimit - 1)){
   vline[j] = ch;
   j++;
  }
  i++;
 }
 if(ch == 'n'){
  while((' ' == vline[--j]) || ('t' == vline[--j]) ){
   i--;
  }
  vline[j] = ch;
  j++;
  i++;
 }
 vline[j] = '0';
 return i;
}

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


Related articles: