在c语言中按字典顺序排列字符串
我想按字典顺序对字符串中的单词进行排序 例如: 我有一个字符串:在c语言中按字典顺序排列字符串,c,arrays,string,sorting,lexicographic,C,Arrays,String,Sorting,Lexicographic,我想按字典顺序对字符串中的单词进行排序 例如: 我有一个字符串:我是苹果 输出应为:am Apple I 问题(输出): 它没有对字符串进行排序,整个字符串也没有显示在输出中,这里有人能帮我吗。谢谢 程序代码: #include<stdio.h> #include<string.h> void main() { char a[25][25],t[25]; char s[200]; char * pch; int count = 0;
我是苹果
输出应为:am Apple I
问题(输出):
它没有对字符串进行排序,整个字符串也没有显示在输出中,这里有人能帮我吗。谢谢
程序代码:
#include<stdio.h>
#include<string.h>
void main()
{
char a[25][25],t[25];
char s[200];
char * pch;
int count = 0;
int i,j ,n;
printf("enter the string\n");
gets(s);
pch = strtok (s," ,.-");
for (i = 0;s[i] != '\0';i++)
{
if (s[i] == ' ')
count++;
}
count=count+1;
i=0;
while(pch != NULL)
{
strcpy(a[i],pch);
pch = strtok (NULL, " ,.-");
i++;
}
for(i=0;i<count-1;i++)
{
for(j=i+1;j<count;j++)
{
if(strcmp(a[i],a[j])>0)
{
strcpy(t,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],t);
}
}
}
printf("the sorted array:\n");
for(i=0;i<count;i++)
printf("%s\n",a[i]);
}
#包括
#包括
void main()
{
字符a[25][25],t[25];
chars[200];
char*pch;
整数计数=0;
inti,j,n;
printf(“输入字符串\n”);
获取(s);
pch=标准公差(s,“,.-”);
对于(i=0;s[i]!='\0';i++)
{
如果(s[i]='')
计数++;
}
计数=计数+1;
i=0;
while(pch!=NULL)
{
strcpy(a[i],pch);
pch=strtok(空,,.-”;
i++;
}
对于(i=0;i使用qsort()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUF_SIZE 0x100
int strcmp_wrapper(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}
int main () {
char buffer[BUF_SIZE], *tokens[BUF_SIZE / 2 + 1];
int i = 0, j = 0;
printf("Enter a string: ");
fgets(buffer, BUF_SIZE, stdin);
tokens[0] = strtok(buffer, " ,.-\n");
while ((tokens[++i] = strtok(NULL, " ,.-\n")));
qsort(tokens, i, sizeof(tokens[0]), strcmp_wrapper);
while (j < i)
printf("%s\n", tokens[j++]);
return 0;
}
#包括
#包括
#包括
#定义BUF_大小0x100
int strcmp_包装(常数无效*a,常数无效*b){
返回strcmp(*(常量字符**)a、*(常量字符**)b);
}
int main(){
字符缓冲区[BUF_大小],*令牌[BUF_大小/2+1];
int i=0,j=0;
printf(“输入字符串:”);
fgets(缓冲区、BUF_尺寸、标准尺寸);
令牌[0]=strtok(缓冲区,,.-\n”);
while((令牌[++i]=strtok(NULL,,.-\n”));
qsort(令牌,i,sizeof(令牌[0]),strcmp_包装器);
而(j
使用qsort()
进行此类操作
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUF_SIZE 0x100
int strcmp_wrapper(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}
int main () {
char buffer[BUF_SIZE], *tokens[BUF_SIZE / 2 + 1];
int i = 0, j = 0;
printf("Enter a string: ");
fgets(buffer, BUF_SIZE, stdin);
tokens[0] = strtok(buffer, " ,.-\n");
while ((tokens[++i] = strtok(NULL, " ,.-\n")));
qsort(tokens, i, sizeof(tokens[0]), strcmp_wrapper);
while (j < i)
printf("%s\n", tokens[j++]);
return 0;
}
#包括
#包括
#包括
#定义BUF_大小0x100
int strcmp_包装(常数无效*a,常数无效*b){
返回strcmp(*(常量字符**)a、*(常量字符**)b);
}
int main(){
字符缓冲区[BUF_大小],*令牌[BUF_大小/2+1];
int i=0,j=0;
printf(“输入字符串:”);
fgets(缓冲区、BUF_尺寸、标准尺寸);
令牌[0]=strtok(缓冲区,,.-\n”);
while((令牌[++i]=strtok(NULL,,.-\n”));
qsort(令牌,i,sizeof(令牌[0]),strcmp_包装器);
而(j
如果尝试在pch=strtok(s,“,.-”)之后打印字符串,您会注意到字符串已被拆分。这是因为strtok()具有破坏性,将字符串拆分为令牌,因此在调用strtok()之前需要计算空格数:
正如Weather Vane所说,不要使用gets(),而是使用fgets(),然后从字符串末尾删除“\n”。此外,您还可以使用realloc()为动态数组而不是静态数组分配更多内存,因为您事先不知道字符串中的字数
#include <stdlib.h>
#include<stdio.h>
#include<string.h>
void main()
{
char** a = NULL;
char t[25];
char s[512];
char * pch;
int count = 0;
int i,j ,n;
printf("enter the string\n");
if(fgets(s,512, stdin)==NULL)
{
printf("failed to read string\n");
exit(-1);
}
/*remove '\n' from end of the string*/
char *pos;
if ((pos=strchr(s, '\n')) != NULL)
*pos = '\0';
pch = strtok(s, " ,.-");
while(pch)
{
a = realloc(a, sizeof(char*)*++count);
if(a==NULL)
{
perror("failed to allocate memory\n");
exit(-1);
}
a[count-1] = pch;
pch = strtok(NULL, " ,.-");
}
for(i=0;i<count;i++)
printf("%d: %s\n", i, a[i]);
///...compare array
#包括
#包括
#包括
void main()
{
字符**a=NULL;
chart[25];
chars[512];
char*pch;
整数计数=0;
inti,j,n;
printf(“输入字符串\n”);
如果(fgets(s,512,stdin)==NULL)
{
printf(“读取字符串失败\n”);
出口(-1);
}
/*从字符串末尾删除“\n”*/
char*pos;
如果((pos=strchr,“\n'))!=NULL)
*pos='\0';
pch=标准公差(s,“,.-”);
while(pch)
{
a=realloc(a,sizeof(char*)*++计数);
如果(a==NULL)
{
perror(“分配内存失败\n”);
出口(-1);
}
a[count-1]=pch;
pch=strtok(空,,.-”;
}
对于(i=0;i如果您尝试在pch=strtok(s,“,.-”)之后打印字符串,您会注意到字符串已被拆分。这是因为strtok()具有破坏性,将字符串拆分为标记,因此在调用strtok()之前需要计算空格数:
正如Weather Vane所说,不要使用gets(),而是使用fgets(),然后从字符串末尾删除“\n”。此外,您还可以使用realloc()为动态数组而不是静态数组分配更多内存,因为您事先不知道字符串中的字数
#include <stdlib.h>
#include<stdio.h>
#include<string.h>
void main()
{
char** a = NULL;
char t[25];
char s[512];
char * pch;
int count = 0;
int i,j ,n;
printf("enter the string\n");
if(fgets(s,512, stdin)==NULL)
{
printf("failed to read string\n");
exit(-1);
}
/*remove '\n' from end of the string*/
char *pos;
if ((pos=strchr(s, '\n')) != NULL)
*pos = '\0';
pch = strtok(s, " ,.-");
while(pch)
{
a = realloc(a, sizeof(char*)*++count);
if(a==NULL)
{
perror("failed to allocate memory\n");
exit(-1);
}
a[count-1] = pch;
pch = strtok(NULL, " ,.-");
}
for(i=0;i<count;i++)
printf("%d: %s\n", i, a[i]);
///...compare array
#包括
#包括
#包括
void main()
{
字符**a=NULL;
chart[25];
chars[512];
char*pch;
整数计数=0;
inti,j,n;
printf(“输入字符串\n”);
如果(fgets(s,512,stdin)==NULL)
{
printf(“读取字符串失败\n”);
出口(-1);
}
/*从字符串末尾删除“\n”*/
char*pos;
如果((pos=strchr,“\n'))!=NULL)
*pos='\0';
pch=标准公差(s,“,.-”);
while(pch)
{
a=realloc(a,sizeof(char*)*++计数);
如果(a==NULL)
{
perror(“分配内存失败\n”);
出口(-1);
}
a[count-1]=pch;
pch=strtok(空,,.-”;
}
下面的for(i=0;i是一种紧凑的工作方式,可以实现您想要的功能。它打印每行的单词,按一个空格进行排序和分隔,而不会重复单词(如果您希望重复这些单词,请确保您能够触摸程序使其工作)
$cat pru799.c
#包括
#包括
#包括
#定义分隔符“\t\n,.-()&%$\”\'[]{}+-*/;:@#|!\\=?”
#定义行大小为1024
#定义最多256个字
int比较(常量字符**p,常量字符**q)
{
返回strcmp(*p,*q);
}
int main()
{
字符行[行大小];
字符*字数[最大字数];
int n_词;
while(fgets(line,sizeof line,stdin)){/*而不是eof*/
char*p;
int i;
/*首先要记住这些话*/
n_字=0;
for(p=strtok(行,分隔符);p;p=strtok(空,分隔符)){
如果(strlen(p)==0)continue;/*字的长度为零*/
如果(n个单词>=最大单词){
fprintf(标准,“超过最大字数(%d)”,最大字数);
退出(退出失败);
}
单词[n_words++]=p;
}/*用于*/
/*现在我们有了字符串数组中的所有单词,对其进行排序*/
qsort(单词,n_单词,单词[0]的大小),(int(*)(const void*,const void*)和compare);
/*现在打印单词*/
for(i=0;i$ cat pru799.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DELIMITERS " \t\n,.-()&%$\"\'[]{}+-*/;:@#|!\\<>=?"
#define LINE_SIZE 1024
#define MAX_WORDS 256
int compare(const char **p, const char **q)
{
return strcmp(*p, *q);
}
int main()
{
char line[LINE_SIZE];
char *words[MAX_WORDS];
int n_words;
while (fgets(line, sizeof line, stdin)) { /* while not eof */
char *p;
int i;
/* first get the words */
n_words = 0;
for (p = strtok(line, DELIMITERS); p; p = strtok(NULL, DELIMITERS)) {
if (strlen(p) == 0) continue; /* word is zero length */
if (n_words >= MAX_WORDS) {
fprintf(stderr, "MAX_WORDS(%d) exceeded\n", MAX_WORDS);
exit(EXIT_FAILURE);
}
words[n_words++] = p;
} /* for */
/* now we have all the words in the array of strings words, sort it */
qsort(words, n_words, sizeof words[0], (int(*)(const void *, const void *))&compare);
/* now print the words */
for (i = 0; i < n_words; i++) {
if (i) { /* all but the first one */
/* don't repeat words */
if (!strcmp(words[i], words[i-1]))
continue;
printf(" "); /* print a space between words */
}
printf("%s", words[i]);
}
printf("\n");
} /* while */
} /* main */