當前位置:首頁 » 編程語言 » c語言單詞按字典排序

c語言單詞按字典排序

發布時間: 2022-05-14 00:47:47

c語言 怎樣將一個英語單詞的字母按字典順序排序

其實和一組數字排序是一樣的道理
用冒泡法給你寫個例子,你可以參考參考。
#include <stdio.h>
#include <string.h>
main()
{
char a[20];
int i,j,str;
char ch;
printf("input a word:\n");
scanf("%s",a);
str=strlen(a);
for(i=0;i<str;i++)
{
for(j=0;j<str-1-i;j++)
{
if(a[j]>a[j+1])
{
ch=a[j];
a[j]=a[j+1];
a[j+1]=ch;
}
}
}
printf("%s\n",a);
}

⑵ C語言編程題:按字典順序排列6個單詞,並且把第一個字母設置為大寫。

其實和一組數字排序是一樣的道理
用冒泡法給你寫個例子,你可以參考參考。
#include
#include
main()
{
char
a[20];
int
i,j,str;
char
ch;
printf("input
a
word:\n");
scanf("%s",a);
str=strlen(a);
for(i=0;i
a[j+1])
{
ch=a[j];
a[j]=a[j+1];
a[j+1]=ch;
}
}
}
printf("%s\n",a);
}

⑶ C語言編程題:輸入N個英文單詞,建立字元串數組,按字典順序輸出這些英文單詞,要求用指針實現。

#include<stdio.h>

#include<string.h>

#defineN5

voidswap(char*p1,char*p2){

chartemp[100];

strcpy(temp,p1);

strcpy(p1,p2);

strcpy(p2,temp);

intmain(){

inti,j;

charstr1[N][100],*str[N];

for(i=0;i<N;i++){

scanf("%s",*(str1+i));

for(i=0;i<N;i++){

str[i]=*(str1+i);

for(i=0;i<N;i++){

for(j=i+1;j<N;j++){

if(strcmp(str[i],str[j])>0){swap(str[i],str[j]);}

for(i=0;i<N;i++){

printf("%s",str[i]);

printf("\n");

return0;

(3)c語言單詞按字典排序擴展閱讀:

printf用法:

printf()函數的調用格式為:printf("&lt;格式化字元串&gt;",&lt;參量表&gt;)。

其中格式化字元串包括兩部分內容:一部分是正常字元,這些字元將按原樣輸出;另一部分是格式化規定字元,以"%"開始,後跟一個或幾個規定字元,用來確定輸出內容格式。

參量表是需要輸出的一系列參數,其個數必須與格式化字元串所說明的輸出參數個數一樣多,各參數之間用","分開,且順序一一對應,否則將會出現意想不到的錯誤。

比如:

inta=1234;

printf("a=%d\n",a);

輸出結果為a=1234。

printf的格式控制的完整格式:

%-0m.nl或h格式字元

下面對組成格式說明的各項加以說明:

①%:表示格式說明的起始符號,不可缺少。

②-:有-表示左對齊輸出,如省略表示右對齊輸出。

③0:有0表示指定空位填0,如省略表示指定空位不填。

④m.n:m指域寬,即對應的輸出項在輸出設備上所佔的字元數。N指精度。用於說明輸出的實型數的小數位數。為指定n時,隱含的精度為n=6位。

⑤l或h:l對整型指long型,對實型指double型。h用於將整型的格式字元修正為short型。

⑷ c語言什麼叫按字典序排序

就是說,將多個字元串的同一位置的字元按照26個字母的順序進行比對。a最小,z最大。

a < b;

aa < ab; 因為第二位置上,前面字元串是a,後面字元串是b,所以是小於關系,以此類推。

⑸ c語言:編寫一個程序找出一組單詞中的「最小「和「最大「的單詞(單詞在字典中的先後順序,字典中先出現

strcmp(largest_word, a);/*這一步沒有運行*/

這一行寫錯了,不是應該strcpy么,不是strcmp。

#include<stdio.h>

#include<string.h>

int main()

{

char ch[5][10];

char min[10],max[10];

int i;

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

{

gets(ch[i]);

}

strcpy(min,ch[0]);

strcpy(max,ch[0]);

for(i=1;i<5;i++)

{

if(strcmp(max,ch[i])<=0)

(5)c語言單詞按字典排序擴展閱讀

C語言的運算符主要用於構成表達式,同一個符號在不同的表達式中,其作用並不一致。下面按計算的優先順序,分別說明不同作用的表達式。需要特別指出,在C語言標准中,並沒有結合性的說法。

相同優先順序運算符,從左至右依次運算。注意後綴運算優先順序高於前綴。因此++i++應解釋為++(i++)。

而與或非的運算優先順序都不一樣,因此a && b || b && c解釋為(a && b) || (b && c)

合理使用優先順序可以極大簡化表達式。

⑹ 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(i<ROWS&&scanf("%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;i<count;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;i<count;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(i<ROWS&&scanf("%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;i<ROWS;i++) pt[i]=a[i];
for(i=0;i<count;i++) for(j=i+1;j<count;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;i<strlen(arr);i++) if(isalpha(arr[i])==0) {
printf("%s不是一個單詞.\n",arr); flag=1; break; }
for(i=0;i<count;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;i<count;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語言 輸入10個單詞,按字典順序輸出 不要復制不對的過來啊

#include"stdio.h"

#include"string.h"

intmain()

{

charstr[10][30]={0};

chartem[30]={0};

inti,j;

printf("請輸入10個單詞:");

for(i=0;i<10;i++)//輸入

scanf("%s",str[i]);

for(i=0;i<9;i++)//排序

for(j=i+1;j<10;j++)

if(strcmp(str[i],str[j])>0)

{

strcpy(tem,str[i]);

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

strcpy(str[j],tem);

}

printf("這10個單詞按照字典排序輸出為: ");

for(i=0;i<10;i++)//輸出

{

printf("%s ",str[i]);

}

}

⑻ c語言 輸入10個單詞 怎麼才能按字典順序輸出 分別用插入排序和歸並排序兩種方法

建立 索引表, 單詞的存儲索引--單詞權值 逐個對應,每個單詞 對應一個權值,每個字母對應 0~25, 單詞的最後字母對應0,向前依次對應 26, 26*26, 26*26*26。。。。, 26進制數,類似於十進制數, 然後 按權值進行排序

⑼ 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;i<num;i++)
{
printf("請輸入第%d個單詞:\n",i+1);
gets(s1[i]);
}

for (i=0;i<num;i++) //按冒泡排序法排序
{
exchange=0;
for(j=0;j<num;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;i<num;i++)
printf("%s\n",s1[i]);
}

⑽ C語言 計算一篇英文文章中單詞出現個數,按英文字典順序輸出.

如果用c++,可以將英文文章保存至主程序所在的文件夾中,可以命名為「a.txt」;不顯示後續名的話命名問「a」;
#include<iostream>
#include<string>
#include<map>
#include<utility>
#include<fstream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
map<string,int> m;
vector<string>vecs;
ifstream in ("a.txt");
string s;
while(in>>s){
m[s]++;
}
auto beg=m.begin();
while(beg!=m.end()){
vecs.push_back(beg->first);
beg++;
}
sort(vecs.begin(),vecs.end());
for(auto c:vecs){
cout<<"單詞:"<<"\t"<<c<<"\t出現的次數是:\t"<<m[c]<<endl;
}
}

熱點內容
日本編程語言 發布:2025-05-11 09:53:52 瀏覽:843
手機店設置的初始密碼一般是多少 發布:2025-05-11 09:33:15 瀏覽:401
昂科威選擇哪個配置 發布:2025-05-11 09:25:50 瀏覽:36
怎麼解決安卓視頻全屏卡頓 發布:2025-05-11 09:14:55 瀏覽:726
匯編從編譯到執行 發布:2025-05-11 09:09:04 瀏覽:258
安卓系統低版本如何升級 發布:2025-05-11 09:04:44 瀏覽:251
認證類型加密演算法 發布:2025-05-11 08:58:35 瀏覽:561
android停靠 發布:2025-05-11 08:42:23 瀏覽:646
超時代加密 發布:2025-05-11 08:41:29 瀏覽:780
為什麼還要輸入支取密碼 發布:2025-05-11 08:32:24 瀏覽:362