Reputation: 75
I have a school assignment to compares strings.
The compare is case insensitive so uppercase or lowercase are the same.
If the strings are equal, then the output is first string. However, if strings aren't equal, then the output is the number of different characters.
Examples:
Input:
string1: LION
string2: lion
Output: LION
Input:
string1: LION
string2: LEON
Output: 1 (because 'I' & 'E' is not equal)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int len, c, d, i, counter=0;
char a[1000],b[1000];
char *temp;
scanf("%[^\n]s", a);
scanf(" %[^\n]s", b);
temp = (char *) malloc(strlen(a));
strcpy(temp,a);
len= strlen(a);
for(i=0;i<len;i++)
{
c= tolower(a[i]);
d= tolower(b[i]);
if(c!=d) counter++;
}
if(counter)
{
printf("%d\n",counter);
} else {
printf("%s\n",temp);
}
free(temp);
return 0;
}
I already created the program but my point only 37.5/100 or i failed 5 test case from 8. So what wrong in my program?
Upvotes: 2
Views: 163
Reputation: 154315
Thought I would toss in another approach that only uses 1 big buffer.
OP's approach of using tolower()
is good. Yet there is no need to copy the string with malloc()
etc. Further, there is no need to retain the 2nd line of input.
Unclear on the programming goal concerning spaces and mis-matched line lengths. This code treats spaces like any other character by reading the line of user input with fgets()
. With different line length inputs, this code ignores the extra of the longer line.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void) {
char a[1000 + 1 + 1]; // +1 for \n, +1 for \0
// scanf("%[^\n]s", a);
if (fgets(a, sizeof a, stdin) == NULL) {
puts("failed to read line");
return (EXIT_FAILURE);
}
a[strcspn(a, "\n")] = '\0'; // trim potential trailing \n
// now compare as code reads
int i = 0;
int counter = 0;
int ch;
while ((ch = fgetc(stdin)) != '\n' && ch != EOF) {
if (a[i]) { // If not at the end of the string
if (tolower((unsigned char) a[i]) != tolower(ch)) { //cast useful w/negative `char`
counter++;
}
i++;
}
}
if (counter) {
printf("%d\n", counter);
} else {
printf("%s\n", a);
}
return 0;
}
Upvotes: 0
Reputation: 14851
First of all, no need to do any manipulation on strings, if the a character differ, another check to see if its because of upper or lower case is done.
Second of all, if string size range is 10-1000
, you need an extra character for the \0
in case a string of size 1000 is tested.
#include <stdio.h>
#include<string.h>
int main()
{
char str1[1001];
char str2[1001];
int iter = 0;
int i = 0;
int diff=0;
int counter = 0;
int len1=0;
int len2=0;
scanf("%s", str1);
scanf("%s", str2);
len1 = strlen(str1);
len2 = strlen(str2);
iter = (len1 > len2)?len2:len1;
for (i=0; i<iter; i++)
{
if (tolower(str1[i]) != tolower(str2[i]))
{
counter++;
}
}
diff = (len1 > len2)?len1:len2;
counter += diff-iter;
printf("%d\n", counter);
}
Input:
LEON
LeON
Output: 0
Input:
LEON
LION
Output: 1
Input:
LEONEE
LION
Output: 3
Upvotes: 0
Reputation: 67751
Your code is hard to understand Two version. first counts the differences and adds the difference in length. Second one only count differences, returns -1 if lengths are not equal Zero returned when strings are equal
size_t my_strcmp(const char *str1, const char *str2)
{
size_t result = abs(((int)strlen(str1) - (int)strlen(str2)));
//or
//int slen1 = strlen(str1);
//int slen2 = strlen(str2);
//int result = slen1 > slen2 ? slen1 - slen2 : slen2 - slen1;
while (*str1 && *str2)
{
if (tolower(*str1++) != tolower(*str2++))
{
result++;
}
}
return result;
}
int my_strcmp(const char *str1, const char *str2)
{
int result = (strlen(str1) != strlen(str2)) * -1;
if (!result)
{
while (*str1)
{
if (tolower(*str1++) != tolower(*str2++))
{
result++;
}
}
}
return result;
}
and in main
int x;
switch((x = my_strcmp(string1, string2)))
{
case -1:
printf("The strings have a different length\n");
break;
case 0:
printf("%s\n", string1);
break;
default:
printf("Number of differences %d\n", x);
break;
}
Upvotes: 2