简体   繁体   English

C中第一个字母的串联,无论大写和小写

[英]Concatenation of first letter alike regardless of upper and lower case in C

i have been figuring out how to combine strings that has first same letter alike regardless of their cases.我一直在弄清楚如何组合具有相同首字母的字符串,而不管它们的大小写。 I have a code that if you input我有一个代码,如果你输入

babe,two,Bird,Tea宝贝,二,鸟,茶

the output is always like this输出总是这样

babe,Bird,Tea,two宝贝,鸟,茶,二

But the input that I want to see is like this但是我想看到的输入是这样的

babeBird,Teatwo宝贝鸟,Teatwo

What am i going to add or change in my code in order for me to do that.我要在我的代码中添加或更改什么才能做到这一点。 Here's my code:这是我的代码:

#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];
    }
  }
}

The following code which decides whether to print a comma以下代码决定是否打印逗号

if (str1[i][0] != letter)

does a case-sensitive comparison.进行区分大小写的比较。

Change it to something like把它改成类似的东西

if (tolower(str1[i][0]) != tolower(letter))

However, the code contains multiple bugs (too many to point them all out), so not sure it will work.但是,该代码包含多个错误(太多而无法全部指出),因此不确定它是否有效。 You might want to do debugging.您可能想要进行调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM