前提・実現したいこと
c言語を勉強しています。
以下の問題を解決したいです。
標準入力から5つの文字列を文字列配列の配列に入力し,その中で最も長い文字列を
出力するプログラムを作成せよ.標準ライブラリ関数を利用すること.
発生している問題・エラーメッセージ
実行結果:
what is your character?
ab
abs
abst
abstru
abstruct
your character's the longest length is abstruct
your character's the longest length is abstru
your character's the longest length is abst
your character's the longest length is abs
your character's the longest length is ab
該当のソースコード
#include <stdio.h> #include <string.h> #define NUM 5 #define LEN 100 void swapstr(char *s1,char *s2)//文字をswap { char tmp[LEN]; strcpy(tmp,s1); strcpy(s1,s2); strcpy(s2,tmp); } int main(void){ char str[NUM][LEN]; int i,j; printf("what is your character?\n"); for(i=0;i<NUM;i++){ scanf("%s",str[i]); } for(i=0;i<NUM;i++){ for(j=i+1;j<NUM+1;j++){ if(strlen(str[i])<strlen(str[j])){ swapstr(str[i],str[j]); } } } printf("your character's the longest length is %s\n",str); return 0; }
試したこと
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答6件
あなたの回答
tips
プレビュー