比较字符串:在一个字符串数组中选出一个,比给定字符串小,但却是小的当中最大的一个字符串。如:abc,123,bdi,dxa,ijf;给定cba,那么符合条件的是:bdi
c,c++实现都可以,我写了一个需要双重循环实现的程序,但我认为单循环就可以实现了,所以想请教一下!为了提高效率急等着解决
char str[10][10]={.....};
char str1="...";
chat *temp;
temp="";
for(int i=0;i<10;i++){
if(strcmp(str[i],str1)==-1 && strcmp(str[i],temp)==1)
temp=str[i];
}
printf("%s\n",temp);
一重循环是可以实现,
我没做过单片机,不了解,下面这个程序您看看,看满足要求不?
#include "stdio.h"
#include "conio.h"
#include "string.h"
int main()
{
char str[5][20]={"abc","123","bdi","dxa","ijf"},source[20]="cba";
char (*p)[20]=&str[0];
int counter=0;
int flag=1;
char obtstr[20];
for(;counter<5;counter++,p++)
{
if(!flag&&strcmp(source,*p)>0)
{
if(strcmp(obtstr,*p)<0)
{
strcpy(obtstr,*p);
}
}
if(flag&&strcmp(source,*p)>0)
{
strcpy(obtstr,*p);
flag=0;
}
}
printf("the string is \"%s\"",obtstr);
getch();
}