How do I trim leading/trailing whitespace in a standard way?

后端 未结 30 1976
一个人的身影
一个人的身影 2020-11-22 02:06

Is there a clean, preferably standard method of trimming leading and trailing whitespace from a string in C? I\'d roll my own, but I would think this is a common problem wit

30条回答
  •  情深已故
    2020-11-22 02:38

    #include "stdafx.h"
    #include "malloc.h"
    #include "string.h"
    
    int main(int argc, char* argv[])
    {
    
      char *ptr = (char*)malloc(sizeof(char)*30);
      strcpy(ptr,"            Hel  lo    wo           rl   d G    eo rocks!!!    by shahil    sucks b i          g       tim           e");
    
      int i = 0, j = 0;
    
      while(ptr[j]!='\0')
      {
    
          if(ptr[j] == ' ' )
          {
              j++;
              ptr[i] = ptr[j];
          }
          else
          {
              i++;
              j++;
              ptr[i] = ptr[j];
          }
      }
    
    
      printf("\noutput-%s\n",ptr);
            return 0;
    }

提交回复
热议问题