"i am a student"变成"student a am i"代码问题,求救:
思路是从字符串末尾开始指针p_source逐渐减一,遇到空格时将p_source赋值给p_word,让p_word把空格后的单词赋值给最终数组dest,代码主要问题在else循环内如何将p_word赋值给dest,可能我对指针还不太熟,帮我看看,谢了,现在运行得到的结果为 "studen ".源代码如下
#include <stdio.h>
#include <string.h>
main()
{
int i,numofword=0;
char source[50]= "I am a student ";
char dest[50];
char * p_word; /*a point to locate a word*/
char * p_source,* p_dest; /*define a point of string source and dest*/
p_dest = dest;
p_source = source+strlen(source)-1; /*p_source point to end of string source*/
for(i=0;i <strlen(source);i++,p_source--) /*p_source move from end of source to the start position*/
{
if(*p_source != ' ') /*fetch a word from position of blank*/
{
numofword++; /*count number of a word*/
/*void that numofword return to zero*/
}
else /*copy the word to string dest*/
{
p_word = p_source;
for(;--numofword!=0; * p_dest = * p_word,p_dest++,p_word++)
{
;
}
p_word = NULL;
numofword = 0;
}
}
* p_dest = '\0 ';
puts(dest);
}
[解决办法]
按照LZ的意思该了下
#include <stdio.h>
#include <string.h>
int main()
{
int i,numofword=0;
char source[50]= "I am a student ";
char dest[50];
char *p_word;/*a point to locate a word*/
char *p_source,*p_dest;/*define a point of string source and dest*/
p_dest = dest;
p_source = source+strlen(source)-1; /*p_source point to end of string source*/
for(i=0;i <(int)strlen(source);i++,p_source--) /*p_source move from end of source to the start position*/
{
if(*p_source != ' ' && *p_source != source[0]) /*fetch a word from position of blank*/
{
numofword++;/*count number of a word*/
/*void that numofword return to zero*/
}
else/*copy the word to string dest*/
{
p_word = p_source+1;
for(; (numofword--) != 0; *p_dest = *p_word,p_dest++,p_word++)
{
;
}
*p_dest++ = *p_source;
p_word = NULL;
numofword = 0;
}
}
*p_dest = '\0 ';
puts(dest);
return 0;
}