1.输入两个字符串,输出其中长度值最大的字符串,要求不用strlen实现编程 2.将数组a中的n个数按反序存放

2025年03月24日 12:59
有2个网友回答
网友(1):

/* dev__c++ 下做的 */
#include
#include

int main(void)
{
char str1[20] ;
char str2[20] ;
int arr[] = {0,1,2,3,4,5,6,7,8,9,10};
int i = 0, j = 0, temp =0;
puts("input the compare string str1 :");
scanf("%s",&str1);
puts("input the compare string str2 :");
scanf("%s",&str2);
while((str1[i] != '\0') && (str2[j] != '\0')) {i++ ;j++;}
printf("i= %d j=%d \n",i ,j );
if(str1[i] != '\0') printf("str1 is longer. str1:\"%s\" str2:\"%s\" ",str1,str2);
else
if(str2[j] != '\0')printf("str2 is longer. str2:\"%s\" str1:\"%s\" ",str2,str1);
else printf("Same. str1:\"%s\" str2:\"%s\" ",str1,str2);
i = (sizeof(arr)/sizeof(arr[0]))-1;
printf("\n the old arr[] :");
for(j=0; j<=i; j++)
printf(" %d",arr[j]);
for(j=i; j>=(i+1)/2; j--)
{
temp = arr[j] ;
arr[j]= arr[i-j];
arr[i-j] = temp;
}
printf("\n the new arr[] :");
for(j=0; j<=i; j++)
printf(" %d",arr[j]);

system("PAUSE");
return 0;
}

网友(2):

1:自己写个strlen不就行了。
2:按反序存放,用递归就行。