Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从输入短语中提取单词,然后使用C按字母顺序获取第一个和最后一个单词?_C_String - Fatal编程技术网

如何从输入短语中提取单词,然后使用C按字母顺序获取第一个和最后一个单词?

如何从输入短语中提取单词,然后使用C按字母顺序获取第一个和最后一个单词?,c,string,C,String,我是C语言编程新手,需要C语言作业的帮助 我有一个字符串输入,使用fgets、苹果香蕉橙梨和我的期望值 输出是第一个单词:苹果,最后一个单词:梨,按字母顺序排列。请帮忙。谢谢 这是我的脚本,抱歉,新的C #include <stdio.h> #include <string.h> int main() { char string[256]; char word0[20]; char word1[20]; char word2[20];

我是C语言编程新手,需要C语言作业的帮助

我有一个字符串输入,使用fgets、苹果香蕉橙梨和我的期望值 输出是第一个单词:苹果,最后一个单词:梨,按字母顺序排列。请帮忙。谢谢

这是我的脚本,抱歉,新的C

#include <stdio.h>
#include <string.h>

int main()
{
    char string[256];
    char word0[20];
    char word1[20];
    char word2[20];
    char word3[20];

    printf( "Enter 5 words seperated by space: " );

    fgets ( string, 256, stdin );

//Trying figure out the scanf part.

//Dignosis inputs.
    printf( "You entered:%s", string );
    printf( "1st word:%s", word0 );
    printf( "2nd word:%s", word1 );
    printf( "3rd word:%s", word2 );
    printf( "4th word:%s", word3 );


//To be doing the comparison here.
//Compare only the first alphabet of the word.
char firstWord[20];
char secondWord[20];

printf(" First word:%s, :ast word:%s",firstWord,secondWord);


getchar();//To pause the script.
}
提示:

1扫描输入字符串,找到分隔符时将单词分隔开

下一个词是Like apple

2找出如何按字母顺序比较两个单词

像苹果和香蕉

3扫描时,按字母顺序记录到目前为止看到的第一个和最后一个单词


就像苹果是迄今为止看到的按字母顺序排列的第一个单词,也是最后一个单词,后来苹果是按字母顺序排列的第一个单词,香蕉是迄今为止看到的按字母顺序排列的最后一个单词。

发布您的尝试和具体问题。到目前为止,您尝试了什么?我们可以帮助你,如果你努力解决作业,你可以学到更多。抱歉。如何让scanf读取字符串中的单词,使用空格作为分隔符?输入5个单词->输入4个单词我知道逻辑,但我不知道如何用C编码。如何在字符串中找到空格?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(){
    char string[256];
    char word[4][20];

    printf( "Enter 4 words seperated by space: " );

    fgets ( string, 256, stdin );

    sscanf(string, "%s %s %s %s",
        word[0], word[1], word[2], word[3]);

//Dignosis inputs.
    printf( "You entered:%s", string );
    printf( "1st word:%s\n", word[0] );
    printf( "2nd word:%s\n", word[1] );
    printf( "3rd word:%s\n", word[2] );
    printf( "4th word:%s\n", word[3] );

    qsort(word, sizeof(word)/sizeof(*word), sizeof(*word), (int (*)(const void*, const void*))strcmp);

    printf("First word:%s, :last word:%s\n", word[0], word[3]);

    getchar();//To pause the script.
    return 0;
}