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;
  
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章