我是C语言编程的新手,我正在尝试编写一种代码,以字母顺序对单词进行排序。我认为大写字母和小写字母不同。我的排序规则是首先考虑字母顺序,然后大写字母优先,而字符较少的单词优先。对于所有单词,我们仅考虑第一个和第二个字母字符,如果它们相似,则转到下一个单词。
最后,当输入0
时,程序应完成。
这是应采取的措施的一个示例:
输入:alireza Mohammad Arash anahita sarah Milad john Alireza Maryam 0
输出:Alireza alireza anahita Arash john Maryan Milad Mohammad sarah
#include<stdio.h>
#include<string.h>
int main() {
int i=0, j=0, count;
char str[25][25], temp[25];
while (1) {
gets(str[i]);
if(str[i][0]=='0')
break;
i++;
j++;
}
count=i;
for(i=0; i<=count; i++)
for(j=i+1; j<=count; j++) {
if(strcmp(str[i], str[j]) > 0) {
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
}
for(i=0; i<count; i++)
printf("%s ", str[i]);
return 0;
}
但是我的代码只能通过比较单词的ASCII代码来对单词进行排序,这会导致所有大写字母排在首位,如
输入:aa bb AA we WE 0
我的输出:AA WE aa bb we
但应该是:
输出:AA aa bb WE we
我在想是否可以做一些如为chars创建新的ASCII码之类的事情,但这似乎也是不可能的。
这样的字符串怎么排序?
答案 0 :(得分:1)
strcasecmp()
count-1
#include<stdio.h>
#include<string.h>
int main(void){
int i=0,j=0,count;
char str[25][25],temp[25];
for (i=0; i < 25; i++) {
if (!fgets(str[i], sizeof str[i], stdin)) break;
if(str[i][0]=='0')break;
str[i][ strcspn(str[i], "\r\n")] = 0;
}
count=i;
for(i=0;i<count;i++) {
for(j=i+1;j<count;j++){
int rc;
rc = strcasecmp(str[i],str[j]);
if (rc < 0) continue;
/* strings are equal, except for case: do the normal compare */
if (rc == 0) rc = strcmp(str[i],str[j]);
if (rc < 0) continue;
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
for(i=0;i<count;i++) {
printf("%s ",str[i]);
}
return 0;
}
更新:新版本,实现了{first_two_characters,长度,rest_of_the字符串} ,使用相当复杂的比较功能:
#include<stdio.h>
#include<string.h>
int myverysillystringcompare(char *ll, char *rr)
{
size_t siz_l, siz_r, siz;
int rc;
siz_l = strlen(ll);
siz_r = strlen(rr);
if (!siz_l || !siz_r) return siz_r - siz_l;
siz = (siz_l < siz_r) ? siz_l : siz_r;
if( siz > 2) siz = 2;
// Compare the first two characters, (if any) case INSIGNIFICANT
rc = strncasecmp( ll, rr, siz );
if (rc) return rc; // They differ
// Compare the first two characters, (if any) case SIGNIFICANT
rc = strncmp( ll, rr, siz );
if (rc) return rc; // they differ
// Compare the lengths; the shortest wins
if (siz_l != siz_r) return siz_l - siz_r;
// Compare the rest of the string, (if any) case INSIGNIFICANT
rc = strcasecmp(ll+siz, rr+siz);
if (rc) return rc; // they differ
// Compare the rest of the string, (if any) case SIGNIFICANT
rc = strcmp(ll+siz, rr+siz);
return rc;
}
int main(void){
int i=0,j=0,count;
char str[25][25],temp[25];
for (i=0; i < 25; i++) {
if (!fgets(str[i], sizeof str[i], stdin)) break;
if(str[i][0]=='0')break;
str[i][ strcspn(str[i], "\r\n")] = 0;
}
count=i;
for(i=0;i<count;i++) {
for(j=i+1;j<count;j++){
int rc;
rc = myverysillystringcompare(str[i],str[j]);
if (rc <= 0) continue;
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
for(i=0;i<count;i++) {
printf("%s ",str[i]);
}
return 0;
}
答案 1 :(得分:0)
您需要根据自己的特定需求量身定制的“新” strcmp()
:
enum /*untagged*/ { AbeforeB = -1, AequalsB = 0, AafterB = 1 };
int tailored_strcmp(const char *a, const char *b) {
static char baseorder[] = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
//if a or b is the empty string
if (*a == 0) return AbeforeB;
if (*b == 0) return AafterB;
int lena = strlen(a);
int lenb = strlen(b);
char *pa = strchr(baseorder, *a);
char *pb = strchr(baseorder, *b);
if (pa == NULL) return lena < lenb ? AbeforeB : AafterB;
if (pb == NULL) return lena < lenb ? AbeforeB : AafterB;
if (pa == pb) {
//need to check second letter
if (a[1] == 0) return AbeforeB;
if (b[1] == 0) return AafterB;
char *ppa = strchr(baseorder, a[1]);
char *ppb = strchr(baseorder, b[1]);
if (ppa == NULL) return lena < lenb ? AbeforeB : AafterB;
if (ppb == NULL) return lena < lenb ? AbeforeB : AafterB;
if (ppa == ppb) return lena < lenb ? AbeforeB : AafterB;
return ppa < ppb ? AbeforeB : AafterB;
}
return pa < pb ? AbeforeB : AafterB;
}
请参见 version running at ideone或version with improved adherence to requirements或 version checking 1-length strings
答案 2 :(得分:-1)
这里的问题是您直接使用Company Country
------- --------
Test Inc. USA
Misc Corp. UK
而不更改第一个字母。因此,即使最后一个字母strcmp
也位于'Z'
之前。以下代码将首字母更改为大写,然后比较字符串,然后将字符串更改为其原始状态。
'a'
此代码仅修复第一个字母,但不检查第二个字母。您可以通过将#include <stdio.h>
#include <string.h>
bool islower(char c) {
return ('a' <= c) && (c <= 'z');
}
void makeupper(char& c) {
c = c - 'a' + 'A';
}
void makelower(char& c) {
c = c - 'A' + 'a';
}
int main() {
int i=0, j=0, count;
char str[25][25], temp[25];
while (1) {
scanf("%s", str[i]);
if(str[i][0]=='0')
break;
i++;
j++;
}
count=i;
// beware for loop conditions
for(i=0; i<count-1; i++)
for(j=i+1; j<count; j++) {
bool lower1 = islower(str[i][0]);
bool lower2 = islower(str[j][0]);
if (lower1)
makeupper(str[i][0]);
if (lower2)
makeupper(str[j][0]);
// swap the strings under these conditions
if (((strcmp(str[i], str[j]) == 0) && lower1 && !lower2) // string are equal but first string's first letter was lowercase and second string's first letter was uppercase
|| (strcmp(str[i], str[j]) > 0)) { // second string comes alphabetically first
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
// undo changes
if(lower1)
makelower(str[j][0]);
if(lower2)
makelower(str[i][0]);
}
else { // undo changes
if (lower1)
makelower(str[i][0]);
if (lower2)
makelower(str[j][0]);
}
}
for(i=0; i<count; i++)
printf("%s ", str[i]);
return 0;
}
和lower1
更改为bool数组,并相应地更改其余代码来完成类似的操作。