输入两个字符串,输出其中长度值大的字符串(要求"不"使用strlen()函数)

如题所述

#include "stdio.h"

int compare(char *a);

int main(){
    char a[100],b[100];
    int len1,len2;

    printf("please input two strings:\n");
    scanf("%s",a);
    scanf("%s",b);
    len1=compare(a);
    len2=compare(b);
    if(len1>=len2){
        printf("最大字符串长度为:%d\n",len1);
    }
    else{
        printf("最大字符串长度为:%d\n",len2);
    }
}

int compare(char *a){
    int i=0;

    while(a[i]!='\0'){
        i++;
    }
    return i;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-04-27
写一个函数,求出长度,然后两个字符串带入函数中,输出它们的长度,比较后输出最大的。本回答被网友采纳
相似回答