[英]Print middle word in a string. C programming
I need help on code that print out a middle word of string. 我需要有关打印出字符串中间单词的代码的帮助。
First Example: 第一个例子:
Input: My name is Alex 输入:我的名字是亚历克斯
Output: either name or is <-- is the middle 输出:名称或<-是中间
Second Example: 第二个例子:
Input: Hello. 输入:您好。 I Need help on my coding. 我需要编码方面的帮助。
Output: Help <-- middle word 输出:帮助<-中间词
Hopefully someone can help me out. 希望有人可以帮助我。 I will try to follow up. 我会尝试跟进。
Here's an idea: 这是一个主意:
You can use strtok()
for the splitting, and perhaps assume that a "typical" sentence has not more than 50 words, each being no longer than 16 characters. 您可以使用strtok()
进行拆分,并假设一个“典型”句子的单词数不超过50个,每个单词不超过16个字符。 Just to simplify the code. 只是为了简化代码。
The full code : 完整代码:
#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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.