#include
int split_str(char* str)//get the count of string
{
int count = 1;
int flag = 0;
while(*str!='\0')
{
if(*str == ' ' && flag == 1)
{
count++;
flag = 0;
}
else if(*str !=' ' && *str != '\0')
{
flag =1;
}
str++;
}
return (count == 1 ? 0 : count);
}
// return -1 when str1 is null or str1 is litter than str2
// return 1 when str2 is null or str2 is litter than str2
// return 0 when str1 is equal as str2
int compare_str(char* str1, char* str2)
{
if(str1 == 0) return -1;
if(str2 == 0) return 1;
while ( *str1 && *str2)
{
if(*str1 > *str2)
{
return 1;
}
else if (*str1 < *str2)
{
return -1;
}
str1++;
str2++;
}
if(*str1 > *str2)
{
return 1;
}
else if (*str1 < *str2)
{
return -1;
}
return 0;
}
void main()
{
char str[200]={0};
gets(str);
int n = split_str(str);
if(n < 1)
{
printf("input error!\n");
return -1;
}
//printf("n=%d\n",n);
char* a[n];//save pointer address
int i;
for(i=0;ia[i]=0;
int index = 0;
int flag = 0;
for(i=0;i<200 || !str[i];i++)
{
if(str[i] != ' ' && flag == 0)
{
a[index++] = &str[i];
flag = 1;
}
else if(str[i] == ' ')
{
flag = 0;
str[i]='\0';
continue;
}
}
int j;
for(i=0;ifor(j=i+1;j if(1 == compare_str(a[i], a[j]))
{
char* tmp;
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
for(i=0;iprintf("%s\n",a[i]);
}
用指针能很好的解决吧。
while(*ptr != '\0')
{
if(*ptr == ' ')
{
temp = *(ptr+1);
}
ptr++;
}
这个我没写完整,大概就是这个思路了。把temp拿出来比较了。
不被采纳的答案啊……删了吧