Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按字母顺序排列C语言句子中的单词?_C - Fatal编程技术网

按字母顺序排列C语言句子中的单词?

按字母顺序排列C语言句子中的单词?,c,C,我试着把单词拼成一个句子并按字母顺序排列。它必须能够区分大写和小写字母,但我很难让它只处理小写字母 如果我一次输入一个单词,它会按字母顺序排列,但一旦我输入多个单词,它的行为就会很奇怪。如果我输入“我需要帮助”,我希望收到“我需要ehlp”;相反,我收到的是“I dnee ehlp” 这是我的密码: #include <stdio.h> #include <string.h> int main (void) { int i, j, k, l=0, m=0, s

我试着把单词拼成一个句子并按字母顺序排列。它必须能够区分大写和小写字母,但我很难让它只处理小写字母

如果我一次输入一个单词,它会按字母顺序排列,但一旦我输入多个单词,它的行为就会很奇怪。如果我输入
“我需要帮助”
,我希望收到
“我需要ehlp”
;相反,我收到的是
“I dnee ehlp”

这是我的密码:

#include <stdio.h>
#include <string.h>

int main (void) 
{
    int i, j, k, l=0, m=0, s=0, N=100;

    printf("input a sentence with no more than 100 characters \n");
    char sentence[N];
    scanf("%[^\n]s", sentence);
    int slength=strlen(sentence);

    printf("Sentence before sorting - %s \n", sentence);
    /*capital string keeps track of position of capital letters*/
    int capital[slength];

    for (i = 0; i < slength-1; i++) 
    {
        for (j = i+1; j < slength; j++) 
        {
        /*make uppercase letters lowercase */
        if (sentence[j-1] <=90 && sentence[j-1]>64)
        {
            capital[l]=i;
            sentence[i]=sentence[i]+32;
        }
        /* skip to next word if a space is detected */
        if(sentence[j]==' ')
        {
            i=j+1;
            j=j+2;
        }
        /*alphabetize loop*/
        if (sentence[i] > sentence[j])
        {
            k = sentence[i];
            sentence[i] = sentence[j];
            sentence[j] = k;
        }
        }   
    }

    printf("Sentence after sorting  - %s \n", sentence);
    return 0;
}
#包括
#包括
内部主(空)
{
int i,j,k,l=0,m=0,s=0,N=100;
printf(“输入一个不超过100个字符的句子\n”);
字符句子[N];
scanf(“%[^\n]s”,句子);
int slength=strlen(句子);
printf(“排序前的句子-%s\n”,句子);
/*大写字符串跟踪大写字母的位置*/
国际资本[长度];
对于(i=0;i句子[j])
{
k=句子[i];
第[i]句=第[j]句;
句子[j]=k;
}
}   
}
printf(“排序后的句子-%s\n”,句子);
返回0;
}

这是一个非常简单的解决方案

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

int letter_sort(const void* a, const void* b)
{
    return tolower(*(const char*)a) - tolower(*(const char*)b);
}

char* sentence_sort(char* s)
{
    char _[strlen(s)+1];
    strcpy(_,s);

    for(char* w = strtok(_, " ."); w; w = strtok(NULL, " ."))
    {
        qsort(w, strlen(w), !!w, letter_sort);
        memcpy(s+(w-_), w, strlen(w));
    }
    return(s);
}


int main(void) {
    char sent[101];

    printf("Input a sentence with no more than 100 characters \n");
    scanf("%[^\n]", sent);

    printf("Sentence before sorting: %s\n", sent);
    printf("Sentence after  sorting: %s\n", sentence_sort(sent));

    return 0;
}

这应该被分解成不同的函数。当使用调试器执行此操作时,您注意到了什么?答案是显而易见的!您应该使用函数
isupper
islower
toupper
tolower
等,而不是将字符与“神奇”数字进行比较。另外,使用
qsort
进行排序。“s”不属于
scanf(“%[^\n]”)
@EugeneSh。如何分离功能?一个用于大写和小写,然后另一个用于排序?1)您需要包括
ctype.h
。2) 你应该检查
scanf()
是否有错误。@Michi:我总是喜欢2行带一个变量,而不是30行带7个变量。@abelenky我希望我知道的足够多,能像你那样快地编写程序,我在这件事上花了3天时间。在信件分拣机中*做什么?我不太熟悉你正在使用的函数,我在课堂上用C第三版解决工程问题,你推荐一本书或一个网站,让我可以阅读更多关于你正在使用的函数的内容吗?任何基本的C书都应该涵盖这段代码中的所有内容
strtok
qsort
是稍微不寻常的功能,但并不多。最困难的部分可能是
memcpy
调用(使用指针数学和偏移量)<代码>*是指针的“反引用”运算符。刚刚发现我不应该使用strtok函数。谢谢你的帮助
Success #stdin #stdout 0s 9424KB
Input a sentence with no more than 100 characters 
Sentence before sorting: The quick Brown fox Jumped over the Lazy Dogs.
Sentence after  sorting: ehT cikqu Bnorw fox deJmpu eorv eht aLyz Dgos.