c语言单词按字典排序
⑴ 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;
}
}