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/zh-tw/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

發表回復

登錄後才能評論