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
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.