c语言排序英文单词,c语言单词排序输出

本文目录一览:

C语言,单词排序,将一篇英语文章出现的单词去掉重复的,并按字母顺序排列

#include stdio.h

#include string.h

#include ctype.h

int main(void)

{

  char *b = “Six hundred years ago, Sir Johan Hawkwood arrived in Italy with a band of soldiers and settled near Florence.”;

  char a[100][20] = {‘\0’};

  char temp[20],temp1[20],temp2[20];

  int i, j, k;

  for (i=0,j=0,k=0; b[i]!=’\0′; i++)

  {

      if(b[i] != ‘ ‘ !ispunct(b[i]))

      {

          a[j][k] = b[i];

          k++;

      }

      else

      {

          j++;

          k=0;

      }

  }

  for (i=0; i=j; i++)

  {

      for (k=i+1; k=j; k++)

      {

          strcpy(temp1, a[i]);

          *temp1 = tolower(*temp1);

          strcpy(temp2, a[k]);

          *temp2 = tolower(*temp2);

          if (strcmp(temp1, temp2) == 1)

          {

              strcpy(temp, a[i]);

              strcpy(a[i], a[k]);

              strcpy(a[k], temp);

          }

          else if (strcmp(temp1, temp2) == 0)

          {

              a[k][0] = ‘\0’;

              k++;

          }

      }

      if(a[i][0] != ‘\0’)

          printf(“%s “, a[i]);

  }

}

C语言:输入6个英文单词,要求按从小到大排序,并输出。如下图所示:

#include stdio.h

#include string.h

int main()

{

char string[10][50], temp[50];

printf(“请输入6个单词:\n”);

for(int i = 0; i 6; i++)

scanf(“%s”, string[i]);

/*冒泡排序*/

for(int i = 0; i 5; i++ )

for(int j = i+1; j 6; j++)

if(strcmp(string[i], string[j]) == 1)//比较字符串大小,可以用strcmp

{

strcpy(temp, string[i]) ;//交换要strcpy

strcpy(string[i], string[j]) ;

strcpy(string[j], temp) ;

}

//输出

printf(“输出排好序的6个单词:\n”);

for(int i = 0; i 6; i++ )

puts(string[i]);

return 0;

}

PS:若有不明白的地方,可以追问

c语言 英文单词排序(函数版)

说明:原题目中的const要删除,否则过不了编译。因为const了就不能排序了…… #include #include “string.h”int GetWords(char *sentence, char *words[]);void SortStrings(char *strs[], int count);//const int main(int argc,char *argv[]){ char str[200]; int nWords = 0; char *words[20]; int i; printf(“input a string: “); gets(str); nWords = GetWords(str,words); SortStrings(words, nWords); puts(“output:”); for(i=0;i0) k=j; if(k-i) p=strs[i],strs[i]=strs[k],strs[k]=p; } /******end******/} 执行结果如下:

用C语言,输入五个英文单词,找出按字母顺序排列在最末尾的输出到屏幕上?

#include stdio.h

#include string.h

main()

{

int i;

char a[5][20]={}, b[20]={};

for(i=0; i5; i++)

gets(a[i]);

strcpy(b, a[0]);

for(i=1; i5; i++)

if(strcmp(b, a[i]) 0)

strcpy(b, a[i]);

printf(“\n”);

puts(b);

}

c语言单词排序

程序第一次运行时,会创建一个“word.txt”(不包括引号)的文本文件,然后要求输入单词。若要退出,请不要点DOS窗口的小叉叉,输入d即可。因为程序在结束之前,对数组中的单词重新排序,并存储到文件中。 #include “stdio.h”---

#include “stdlib.h” ---为exit()函数提供原型; #include “string.h”---字符串处理函数原型; #include “ctype.h”---字符处理函数原型; #define ROWS 256

#define COLS 32---定义“字典”的大小:可存放256个单词,每个单词的长度不超过31

static FILE *fp;---定义文件指针:内部链接,文件作用域;

static char a[ROWS][COLS];---定义数组:内部链接,文件作用域;该数组的作用是将文件的内容复制进来,并加以处理。因为处理数组比处理文件方便。

char get_option(void);---接收用户的选项,防止误操作。若输入“a;”(不包括引号),那么将视为选项a

int b(int count);---完成选项b的作用--接收新单词;

void c(char *pt[], int count);---完成选项c的作用--通过指针对数组排序,实际数组元素位置未改变;

int check(char arr[], int count);---对输入的单词进行分辨,若输入 ni hao ,将视为单词 ni ,并且提示并剔除重复的单词;

void storage(char *pt[], int count);---在程序结束之前重新排序存储数组中的单词到文件中。

#include “stdio.h” #include “stdlib.h” #include “string.h” #include “ctype.h” #define ROWS 256 #define COLS 32 static FILE *fp;

static char a[ROWS][COLS]; char get_option(void); int b(int count);

void c(char *pt[], int count); int check(char arr[], int count); void storage(char *pt[], int count); int main(void) {

int i,count; int start;

char *pt[ROWS]; char ch, len; char input;

if((fp=fopen(“words.txt”,”a+”))==NULL) {

fputs(“不能打开或建立文件!\n”,stderr); exit(1); }

fseek(fp,0L,SEEK_END); start=(int)ftell(fp)/32; count=start; rewind(fp);

if(fread(a,32*sizeof(char),start,fp)==0) { i=0;

puts(“开始创建词库”);

puts(“请输入单词(每行一个)”);

puts(“在新行输入END结束输入:”); while(iROWSscanf(“%s”, a[i])==1) {

fflush(stdin);

if(strncmp(a[i],”END”,3)==0) {

count+=i; break;

}

if(check(a[i], i)) continue; i++; } }

puts(“\t\t*********************欢迎使用字典排版系统*******************\n\n”);

puts(” MENU “); puts(“您要做些什么?”);

puts(“a. 显示已有的单词 b. 添加新单词”); puts(“c. 对已有的单词进行排序 d. 退出”);

puts(“\n\n\t\t**********************************************************\n”); while((input=get_option())!=’d’)

{

if(input==’a’) { puts(“已有的单词:”); for(i=0;icount;i++)

{

printf(” “); puts(a[i]); } }

if(input==’b’)

{

puts(“开始创建词库”);

puts(“请输入新的单词(每行一个)”); puts(“在新行输入END结束输入: “); count=b(count); }

if(input==’c’) {

puts(“对单词进行排序:”); c(pt, count);

for(i=0;icount;i++) {

printf(” “); puts(pt[i]); } }

puts(“还要做些什么?”); }

storage(pt,count); fclose(fp);

puts(“谢谢使用,再见!”);

return 0; }

char get_option(void) {

char ch;

while((ch=getchar())’a’||ch’d’) {

while((ch=getchar())!=’\n’) ;

puts(“请输入a,b,c或者d.”); }

fflush(stdin);

return ch; }

int b(int count) { int i;

i=count;

while(iROWSscanf(“%s”, a[i])==1) {

fflush(stdin); if(check(a[i], i)) continue;

if(strncmp(a[i],”END”,3)==0) {

count=i; break; } i++; }

return count; }

void c(char *pt[], int count) { int i,j;

char *temp;

for(i=0;iROWS;i++) pt[i]=a[i];

for(i=0;icount;i++) for(j=i+1;jcount;j++) {

if(strcmp(pt[i],pt[j])0) {

temp=pt[i]; pt[i]=pt[j]; pt[j]=temp; } } }

int check(char arr[], int count) { int i;

int flag=0;

for(i=0;istrlen(arr);i++) if(isalpha(arr[i])==0) {

printf(“%s不是一个单词.\n”,arr); flag=1; break; }

for(i=0;icount;i++)

if(strncmp(a[i],a[count],strlen(a[count])+1)==0) {

puts(“重复的单词!”); flag=1; }

return flag; }

void storage(char *pt[], int count) { int i,j;

char ptr[ROWS][COLS];

c(pt, count);

for(i=0;icount;i++)

for(j=0;pt[i][j]!=’\0′;j++) ptr[i][j]=pt[i][j];

fp=fopen(“words.txt”,”w+”); rewind(fp);

fwrite(ptr,32*sizeof(char),count,fp); }

C语言编程:英文单词怎么按A~~z的方法排序

/*字符串冒泡排序,以输入的字符串为空格为结束*/

#include stdio.h

#include string.h

#define MAXNUM 5

#define MAXLEN 20

main()

{

char s1[MAXNUM][MAXLEN],max[MAXLEN];

int num=MAXNUM,i,j,exchange;

for (i=0;inum;i++)

{

printf(“请输入第%d个单词:\n”,i+1);

gets(s1[i]);

}

for (i=0;inum;i++) //按冒泡排序法排序

{

exchange=0;

for(j=0;jnum;j++)

if (strcmp(s1[j],s1[j+1])0)

{

strcpy(max,s1[j]);

strcpy(s1[j],s1[j+1]);

strcpy(s1[j+1],max);

exchange=1;

}

if(!exchange)

break;

}

printf(“按大小输出单词:\n”);

for (i=0;inum;i++)

printf(“%s\n”,s1[i]);

}

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/187691.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-11-28 06:24
下一篇 2024-11-28 06:24

相关推荐

  • AES加密解密算法的C语言实现

    AES(Advanced Encryption Standard)是一种对称加密算法,可用于对数据进行加密和解密。在本篇文章中,我们将介绍C语言中如何实现AES算法,并对实现过程进…

    编程 2025-04-29
  • 学习Python对学习C语言有帮助吗?

    Python和C语言是两种非常受欢迎的编程语言,在程序开发中都扮演着非常重要的角色。那么,学习Python对学习C语言有帮助吗?答案是肯定的。在本文中,我们将从多个角度探讨Pyth…

    编程 2025-04-29
  • Python被称为胶水语言

    Python作为一种跨平台的解释性高级语言,最大的特点是被称为”胶水语言”。 一、简单易学 Python的语法简单易学,更加人性化,这使得它成为了初学者的入…

    编程 2025-04-29
  • OpenJudge答案1.6的C语言实现

    本文将从多个方面详细阐述OpenJudge答案1.6在C语言中的实现方法,帮助初学者更好地学习和理解。 一、需求概述 OpenJudge答案1.6的要求是,输入两个整数a和b,输出…

    编程 2025-04-29
  • Python按位运算符和C语言

    本文将从多个方面详细阐述Python按位运算符和C语言的相关内容,并给出相应的代码示例。 一、概述 Python是一种动态的、面向对象的编程语言,其按位运算符是用于按位操作的运算符…

    编程 2025-04-29
  • Python语言由荷兰人为中心的全能编程开发工程师

    Python语言是一种高级语言,很多编程开发工程师都喜欢使用Python语言进行开发。Python语言的创始人是荷兰人Guido van Rossum,他在1989年圣诞节期间开始…

    编程 2025-04-28
  • Python语言设计基础第2版PDF

    Python语言设计基础第2版PDF是一本介绍Python编程语言的经典教材。本篇文章将从多个方面对该教材进行详细的阐述和介绍。 一、基础知识 本教材中介绍了Python编程语言的…

    编程 2025-04-28
  • Python语言实现人名最多数统计

    本文将从几个方面详细介绍Python语言实现人名最多数统计的方法和应用。 一、Python实现人名最多数统计的基础 1、首先,我们需要了解Python语言的一些基础知识,如列表、字…

    编程 2025-04-28
  • Python作为中心语言,在编程中取代C语言的优势和挑战

    Python一直以其简单易懂的语法和高效的编码环境而著名。然而,它最近的发展趋势表明Python的使用范围已经从脚本语言扩展到了从Web应用到机器学习等广泛的开发领域。与此同时,C…

    编程 2025-04-28
  • Python基础语言

    Python作为一种高级编程语言拥有简洁优雅的语法。在本文中,我们将从多个方面探究Python基础语言的特点以及使用技巧。 一、数据类型 Python基础数据类型包括整数、浮点数、…

    编程 2025-04-28

发表回复

登录后才能评论