文字列の中間の単語を出力するコードについてサポートが必要です。
最初の例:
入力:私の名前はアレックスです
出力:名前または<-のいずれかが真ん中です
2番目の例:
入力:こんにちは。コーディングについてサポートが必要です。
出力:ヘルプ<-ミドルワード
うまくいけば、誰かが私を助けることができます。フォローアップを試みます。
ここにアイデアがあります:
分割に使用できますstrtok()
。おそらく、「一般的な」文の単語数は50語以下で、それぞれが16文字以下であると想定できます。コードを単純化するためだけに。
完全なコード:
#include <stdio.h>
#include<string.h>
#include<conio.h>
int main(){
char str[] = "good men to come to the aid of their country";
char delims[] = " ";
char *result;
char word[100][100];
int loop=1;
result = strtok(str, delims );
strcpy(word[0],result);
while( result != NULL ) {
loop++;
strcpy(word[loop],result);
result = strtok( NULL, delims );
}
int mid=loop/2-1;
// to print the middle element
printf("word is %s \n",word[mid+1]);
getch();
return 0;