Remove extra white spaces in C++

前端 未结 12 1806
日久生厌
日久生厌 2021-02-05 12:01

I tried to write a script that removes extra white spaces but I didn\'t manage to finish it.

Basically I want to transform abc sssd g g sdg gg gf into

12条回答
  •  一整个雨季
    2021-02-05 12:15

    Simple program to remove extra white spaces without using any inbuilt functions.

    #include
    #include
    #include
    using namespace std;
    
    int main()
    {
      char str[1200];
      int i,n,j,k, pos = 0 ;
      cout<<"Enter string:\n";
      gets(str);
      n = strlen(str);
      for(i =0;i<=n;i++)
      {
          if(str[i] == ' ')
          {
              for(j= i+1;j<=n;j++)
              {
                      if(str[j] != ' ')
                      {
                          pos = j;
                          break;
                      }
               }
             if(pos != 0 && str[pos] != ' ')
             {
                for(k =i+1;k< pos;k++)
                 {   if(str[pos] == ' ')
                         break;
                     else{
                        str[k] = str[pos];
                        str[pos] = ' ';
                        pos++;
                     }
    
                 }
             }
    
          }
      }
      puts(str); 
    }
    

提交回复
热议问题