this is a gaga return into gaga a is this

例如:(唉~~,实践知识到用时,方恨少啊。。。回家老老实实写了个半小时才写完的。。。可能边界情况没有考虑充分。。。)

 

int _tmain(int argc, _TCHAR* argv[])
{
 char *str = (char*)malloc(15);
 char *str_words[MAXLEN], *p_str = str;
 strcpy(str,"this is a dog");
 int word_num = readwords(str, str_words);
 int tmp_cnt =0, char_num = 0;
 for (; tmp_cnt < word_num; tmp_cnt++ )

 {
          char_num = strlen(str_words[word_num-tmp_cnt-1]);
          memcpy(str, str_words[word_num-tmp_cnt-1],char_num);
          str +=char_num;
        *str++ = ' ';
  }    
 *str = '/0';
 cout<<p_str<<endl;
 return 0;
}

 

/* str is the words array, str_words is the output arrays  */
int readwords(char * str, char *str_words[])
{
 int  words_cnt = 0;
 int word_len = 0;
 char *p  = NULL;
 char word[MAXLEN];
  
 /*later for the conner case */
 if ( NULL == str )
 {
       return 0;
 }
 word_len = getword(word,str);
 
 p = (char*)malloc(word_len);
 strcpy(p, word);
 str_words[words_cnt++] = p;

 str += (word_len - 1);
 while( *str != '/0')
 {
      if((*str == ' ') || (*str == '/t'))
      {
             word_len = getword(word,++str);

             p = (char*)malloc(word_len);
             strcpy(p, word);
             str_words[words_cnt++] = p;

             str += (word_len - 1);
      }
      else
     {
             str++;
      }
   }
 return words_cnt;
}

 

int getword(char *word, char *str)
{
   int word_len = 0;
  
   if(NULL == str)
   {
          return 0;
   }

   while((*str != ' ')&&(*str != '/0'))
   {
          word_len++;
        *word++ = *str++;
   }

   if((*str == ' ') || ( *str == '/0'))
   {
         *word = '/0';
           word_len++;
   }
   return word_len;
  
}

你可能感兴趣的:(this is a gaga return into gaga a is this)