我一直在研究如何组合首字母相同的字符串,无论它们的大小写如何。我有一个代码,如果您输入
婴儿,两个,鸟,茶
输出总是像这样
婴儿,鸟,茶,两个
但是我想看到的输入是这样的
babeBird,Teatwo
我将在我的代码中添加或更改什么,以便我做到这一点。这是我的代码:
#include<stdio.h>
#include<string.h>
#include <ctype.h>
int main()
{
char str1[1000][1000], str[1000], temp[1000];
int n, i, p, j, a;
char *ptr, *ptr1, letter;
printf("Enter how many arrays: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Enter string %d: ", i+1);
scanf("%s", &str1[i]);
}
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if (tolower((unsigned char)str1[i][0]) == tolower((unsigned char)str1[j][0])
){
strcpy(temp, str1[i]);
strcpy(str1[i], str1[j]);
strcpy(str1[j], temp);
}
if(strcasecmp(str1[i], str1[j])>0)
{
strcpy(temp, str1[i]);
strcpy(str1[i], str1[j]);
strcpy(str1[j], temp);
}
}
}
for(i=0; i<n; i++)
{
if (i != 0)
{
else if (str1[i][0] != letter)
{
printf(",");
}
}
{
printf("%s", str1[i]);
letter = str1[i][0];
}
}
}
下面的代码决定是否打印逗号
if (str1[i][0] != letter)
进行区分大小写的比较。
将其更改为类似
if (tolower(str1[i][0]) != tolower(letter))