本文目錄一覽:
- 1、C語言,單詞排序,將一篇英語文章出現的單詞去掉重複的,並按字母順序排列
- 2、C語言:輸入6個英文單詞,要求按從小到大排序,並輸出。如下圖所示:
- 3、c語言 英文單詞排序(函數版)
- 4、用C語言,輸入五個英文單詞,找出按字母順序排列在最末尾的輸出到屏幕上?
- 5、c語言單詞排序
- 6、C語言編程:英文單詞怎麼按A~~z的方法排序
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