我是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.
}
#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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句