0

So i have to make a game/program that looks something like this:

Enter a sentence, Neo: A a Black blACk Cat cAT is in the xirtaM.

Converted sentence: A Black Cat is in the Matrix.

However when I do it, it literally reprints the same A a Black blACk Cat cAT is in the xirtaM. sentence over again I dont think its reading the whole uppercases' in the middle of the word or something. my code is

printf ("Enter a sentence, Neo: \n");
gets (str);

// let us convert the string into 2D array
for (i = 0; str[i] != '\0'; i++)
{
    if (str[i] == ' ')
    {
        twoD[k][j] = '\0';
        k ++;
        j = 0;
    }
    else
    {
        twoD[k][j] = str[i];
        j ++;
    }
}

twoD[k][j] = '\0';

j = 0;
for (i = 0; i < k; i++)
{
    int present = 0;
    for (l = 1; l < k + 1; l++)
    {
        if (twoD[l][j] == '\0' || l == i)
        {
            continue;
        }

        if (strcmp (twoD[i], twoD[l]) == 0) {
            twoD[l][j] = '\0';
            present = present + 1;
        }
    }
    // if (present > 0)      | uncomment this `if` block if you
    // {                 | want to remove all the occurrences 
    //  twoD[i][j] = '\0';   | of the words including the word
    // }                 | itself.
}

j = 0;

for (i = 0; i < k + 1; i++)
{
    if (twoD[i][j] == '\0')
        continue;
    else
        printf ("%s ", twoD[i]);
}

printf ("\n");
return 0;
ibo salaj
  • 11
  • 2
  • 1
    where's the part where your program fixes the uppercase? – user253751 Nov 18 '20 at 15:38
  • provide little more info on what is the general rule it supposed to work, in the example some words are repeated and some are not, is always first three or can vary etc? – IrAM Nov 18 '20 at 15:46
  • by the way you should avoid [gets](https://stackoverflow.com/questions/2843073/warninggets-function-is-dangerous#:~:text=As%20Wikipedia%27s%20article%20says%2C%20gets%20%28%29%20is%20inherently,allocated%20to%20that%20char%20%2A%20in%20any%20situation.) – IrAM Nov 18 '20 at 16:11

1 Answers1

0

The issue is simple. The strcmp() function in C is case sensitive. So the function will treat the words differently.

Run your str contents through tolower() or something like that to make everything lower case.


.
.
gets(str);
for(int i=0;str[i]!='\0';i++)str[i]=tolower(str[i]);
.
.
.

Then it should be working.

  • Getting a compiling error gets (str); for(int i=0;str[i]!='\0';i++)str[i]=tolower(str[i]);} it works if i remove the for(int i=0;str[i]!='\0';i++)str[i]=tolower(str[i]);} line – ibo salaj Nov 18 '20 at 15:59
  • You might have to include string.h and compile in C99 mode. –  Nov 18 '20 at 17:25
  • @ibosalaj I tested it with your code in my system so it's good –  Nov 18 '20 at 17:26