把*pname分成两个字符串,以空格符为界,
赋给*surname和 *firstname.
for(;*pname!=' ';pname++)
{
*surname=*pname;
surname++;
}
pname++;
for(;*pname;pname++)
{
*firstname=*pname;
firstname++;
}
本人调试的时候有问题,请高手指点。
13 个解决方案
#1
要是中间空格不止一个呢?那第二个循环不是不起作用了?
#2
for(;*pname;pname++)
{
*firstname=*pname;
firstname++;
}
你的pname是以'\0'结尾的吗?
surname、firstame分配了内存了吗
{
*firstname=*pname;
firstname++;
}
你的pname是以'\0'结尾的吗?
surname、firstame分配了内存了吗
#3
include <string.h>
char * strtok(char *sString,char *sToken);
*s1=strtok(s1," ");
*s2=strtok(NULL," ");
就可以把 s1分成两部分了。
char * strtok(char *sString,char *sToken);
*s1=strtok(s1," ");
*s2=strtok(NULL," ");
就可以把 s1分成两部分了。
#4
while (*(pname++) !=' ')
*(surname++)=*pname;
*surname = '\0';
while (*(pname++) !=' ')
;
while (*pname != '\0')
*(firstname++)=*(pname++);
*firstname = '\0'
*(surname++)=*pname;
*surname = '\0';
while (*(pname++) !=' ')
;
while (*pname != '\0')
*(firstname++)=*(pname++);
*firstname = '\0'
#5
还不给分呀
#6
#include <stdio.h>
void fun(char *pname, char *surname, char *firstname)
{
while((*firstname++ = *pname++) != ' ');
*(firstname-1) = 0x00;
while((*surname++ = *pname++) != 0x00);
return;
}
void main()
{
char *pname = "Murry Sherk";
char surname[20];
char firstname[20];
fun(pname, surname, firstname);
printf("%s, %s, %s\n", pname, surname, firstname);
}
Murry Sherk, Sherk, Murry
Press any key to continue
void fun(char *pname, char *surname, char *firstname)
{
while((*firstname++ = *pname++) != ' ');
*(firstname-1) = 0x00;
while((*surname++ = *pname++) != 0x00);
return;
}
void main()
{
char *pname = "Murry Sherk";
char surname[20];
char firstname[20];
fun(pname, surname, firstname);
printf("%s, %s, %s\n", pname, surname, firstname);
}
Murry Sherk, Sherk, Murry
Press any key to continue
#7
用sscanf解决不是很省事吗?
或者用strtok也行。
或者用strtok也行。
#8
我认为:因该这样:
char*surname=NULL;
char*firstname=NULL;
for(;*pname!=' ';)
{
*firstname=*pname;
pname++;
firstname++;
}
firstname=NULL;
pname++;
strcpy(surname,pname);
char*surname=NULL;
char*firstname=NULL;
for(;*pname!=' ';)
{
*firstname=*pname;
pname++;
firstname++;
}
firstname=NULL;
pname++;
strcpy(surname,pname);
#9
不好意思,刚才太紧张,漏了一步
我觉得对第二个字符串的赋值用 strcpy()方便
char surname[40];
char firstname[40];
surname=NULL;
firstname=NULL;
while(*pname!=' ')
{
*firstname=*pname;
pname++;
firstname++;
}
firstname=NULL;
pname++;
strcpy(surname,pname);
我觉得对第二个字符串的赋值用 strcpy()方便
char surname[40];
char firstname[40];
surname=NULL;
firstname=NULL;
while(*pname!=' ')
{
*firstname=*pname;
pname++;
firstname++;
}
firstname=NULL;
pname++;
strcpy(surname,pname);
#10
哎~
#11
搞什么啊!用strtok()不就搞定啦,哪来那么多废话!去看看帮助找strtok
#12
char *substr;
substr=strtok(char *dest," ")
while(substr!=NULL)
{
substr=strtok(NULL," ");
}
substr=strtok(char *dest," ")
while(substr!=NULL)
{
substr=strtok(NULL," ");
}
#13
既然用指针,为什么不这样做呢???
surname=pname
while((*pname!=' ')&&(*pname!='\0'))
pname++;
firstname=pname;
if(*firstname!=NULL)
firstname++; 越过空格
*pname='\0;'
surname=pname
while((*pname!=' ')&&(*pname!='\0'))
pname++;
firstname=pname;
if(*firstname!=NULL)
firstname++; 越过空格
*pname='\0;'
#1
要是中间空格不止一个呢?那第二个循环不是不起作用了?
#2
for(;*pname;pname++)
{
*firstname=*pname;
firstname++;
}
你的pname是以'\0'结尾的吗?
surname、firstame分配了内存了吗
{
*firstname=*pname;
firstname++;
}
你的pname是以'\0'结尾的吗?
surname、firstame分配了内存了吗
#3
include <string.h>
char * strtok(char *sString,char *sToken);
*s1=strtok(s1," ");
*s2=strtok(NULL," ");
就可以把 s1分成两部分了。
char * strtok(char *sString,char *sToken);
*s1=strtok(s1," ");
*s2=strtok(NULL," ");
就可以把 s1分成两部分了。
#4
while (*(pname++) !=' ')
*(surname++)=*pname;
*surname = '\0';
while (*(pname++) !=' ')
;
while (*pname != '\0')
*(firstname++)=*(pname++);
*firstname = '\0'
*(surname++)=*pname;
*surname = '\0';
while (*(pname++) !=' ')
;
while (*pname != '\0')
*(firstname++)=*(pname++);
*firstname = '\0'
#5
还不给分呀
#6
#include <stdio.h>
void fun(char *pname, char *surname, char *firstname)
{
while((*firstname++ = *pname++) != ' ');
*(firstname-1) = 0x00;
while((*surname++ = *pname++) != 0x00);
return;
}
void main()
{
char *pname = "Murry Sherk";
char surname[20];
char firstname[20];
fun(pname, surname, firstname);
printf("%s, %s, %s\n", pname, surname, firstname);
}
Murry Sherk, Sherk, Murry
Press any key to continue
void fun(char *pname, char *surname, char *firstname)
{
while((*firstname++ = *pname++) != ' ');
*(firstname-1) = 0x00;
while((*surname++ = *pname++) != 0x00);
return;
}
void main()
{
char *pname = "Murry Sherk";
char surname[20];
char firstname[20];
fun(pname, surname, firstname);
printf("%s, %s, %s\n", pname, surname, firstname);
}
Murry Sherk, Sherk, Murry
Press any key to continue
#7
用sscanf解决不是很省事吗?
或者用strtok也行。
或者用strtok也行。
#8
我认为:因该这样:
char*surname=NULL;
char*firstname=NULL;
for(;*pname!=' ';)
{
*firstname=*pname;
pname++;
firstname++;
}
firstname=NULL;
pname++;
strcpy(surname,pname);
char*surname=NULL;
char*firstname=NULL;
for(;*pname!=' ';)
{
*firstname=*pname;
pname++;
firstname++;
}
firstname=NULL;
pname++;
strcpy(surname,pname);
#9
不好意思,刚才太紧张,漏了一步
我觉得对第二个字符串的赋值用 strcpy()方便
char surname[40];
char firstname[40];
surname=NULL;
firstname=NULL;
while(*pname!=' ')
{
*firstname=*pname;
pname++;
firstname++;
}
firstname=NULL;
pname++;
strcpy(surname,pname);
我觉得对第二个字符串的赋值用 strcpy()方便
char surname[40];
char firstname[40];
surname=NULL;
firstname=NULL;
while(*pname!=' ')
{
*firstname=*pname;
pname++;
firstname++;
}
firstname=NULL;
pname++;
strcpy(surname,pname);
#10
哎~
#11
搞什么啊!用strtok()不就搞定啦,哪来那么多废话!去看看帮助找strtok
#12
char *substr;
substr=strtok(char *dest," ")
while(substr!=NULL)
{
substr=strtok(NULL," ");
}
substr=strtok(char *dest," ")
while(substr!=NULL)
{
substr=strtok(NULL," ");
}
#13
既然用指针,为什么不这样做呢???
surname=pname
while((*pname!=' ')&&(*pname!='\0'))
pname++;
firstname=pname;
if(*firstname!=NULL)
firstname++; 越过空格
*pname='\0;'
surname=pname
while((*pname!=' ')&&(*pname!='\0'))
pname++;
firstname=pname;
if(*firstname!=NULL)
firstname++; 越过空格
*pname='\0;'