Is it possible to compare a char index to a literal?

Basically I am unable to enter this one case.

 else if(stringed[COUNT] == '\0' )
        {
              Serial.print(" DEBUG - Enters else if case");
              stringed[COUNT+1] = toUpperCase(stringed[COUNT+1]);
              COUNT++;
        }

(stringed[COUNT] == '\0' )

where "stringed" is a char data type and "COUNT" is an index number.

I tried using strcmp(stringed[COUNT],'\0') == 0), with no such luck.

Overall I am trying capitalize the first letter of each word. I tested the inside part and it works I am just not able to enter this specific case.

So my question is Is it possible to compare a character that resides in a char[index] to a literal?
If yes, what is the solution?

what is '\0'?

why not just 0 (i.e. NUL)

my apologies. Its ascii for null character, like a blank. the backslash is to escape.

edit:let me try just 0, and see if that works

edit2: no that did not work

yes

char stringed [] = "myString";

    switch (strings [i])  {
    case 'a':
       break;
    case '\n'
        break;
    case 0:
        break;
    default:
        break;
    }

gcjr:
what is '\0'?

why not just 0 (i.e. NUL)

Because it has a special meaning (terminator) which aids in reading comprehension, even if it really has the value 0. I think it also forces it to be a char literal, vs an int literal (which may not matter in most cases). It's been in standard usage in C since the beginning, so it's perfectly acceptable to use.

I just tried this too:

else if(strncmp(stringed[COUNT], '\0', 1) == 0 )

perhaps I am just not familiar with my options

The second argument in strncmp() is supposed to be a pointer to a char array, not a char literal.

The expression (stringed[COUNT] == '\0' ) looks perfectly good. You have an else-if, maybe the if that precedes it is always chosen, then the code in question would not be executed.

If I understand correctly, you are taking the lack of debug print as evidence that the expression is not evaluating to 'true'. In fact, that isn't acceptable proof because it can happen for other reasons such as the one I mention above.

I feel you. Im just exhausting all my options before strategizing.

My next option would be to attempt to check each char before it went into my char when it was reading through the serial port and try to capitalize it there before storing, and just skip this case.

Ok I tried to do it character by character by storing the previous character. but still no success this route.

Several things need to happen, which is why the block is so large. Here is my logic flow

  • reads a string inputted into the serial monitor
  • reads character by character
  • checks for the number of vowels (including y, generic definition)
  • checks for the number of words
  • needs to uppercase first letter of each word
  • store all the chars into a string, i.e. char to be displayed

disregard the flags for a moment that just enables disable other functions, my problem is isolated to this block alone.

if(DISPLAYFLAG == 1 && Serial.available() > 0) // Function 5
 {
     
     inputchar = Serial.read(); // reads charcater by character
     
     // counts Vowels
     if(inputchar == 'a' ||inputchar == 'A' ||inputchar == 'e' ||inputchar == 'E' ||inputchar == 'i' ||inputchar == 'I' ||
     inputchar == 'o' ||inputchar == 'O' ||inputchar == 'u' ||inputchar =='U' ||inputchar == 'y' ||inputchar == 'Y' )
     {
       VCOUNT++;
     }    
     // counts words
     if(inputchar == '\0' || inputchar == '\r') 
     {
        WCOUNT++;
     }
    
    // checks for previous char being null
    if(inputcharPrev == '\0') 
    {
      stringed[COUNT] = toUpperCase(inputchar);
    }
    else // stores char as is
    {
      stringed[COUNT] = inputchar;
    }
    
    inputcharPrev = inputchar; // stores previous character
    COUNT++; // increments counter
     
     // checks for return carriage, resets flags
     if(inputchar == '\r') 
     {
       DISPLAYFLAG = 0;
       UPPERFLAG = 1;
       COUNT = 0;
     }
     
 }
  // Uppercase each word
      if(UPPERFLAG == 1)
      {
        if(COUNT == 99)// I purposly set this to an unreachable state to try my new strategy
        {
          stringed[COUNT] = toUpperCase(stringed[COUNT]);
          COUNT++;
        }
        /*else if(stringed[COUNT] == '\0')
        {
         stringed[COUNT+1] = toUpperCase(stringed[COUNT+1]);
          COUNT++;
        }*/
        else
        { COUNT++;
          Serial.print("\nWord Count: ");
          Serial.print(WCOUNT);
          Serial.print("\nVowel Count: ");
          Serial.print(VCOUNT);
          Serial.print("\n");
          Serial.print(stringed);
          Serial.print("\n");
          UPPERFLAG = 0;
          COUNT = 0;
          Display();
          VCOUNT = 0;
          WCOUNT = 0;
        }
        
      }

As of now if I type “hello world” into the serial monitor
it spits out “Hello world”, all my counters(vcount, wcount, count) seem to work fine
But its not capitalizing the second(third fourth etc) word

aarg:
If I understand correctly, you are taking the lack of debug print as evidence that the expression is not evaluating to ‘true’. In fact, that isn’t acceptable proof because it can happen for other reasons such as the one I mention above.

I see what you are saying. i shall go check my cases again. At one point I was checking if the next index was an alpha using isAlpha(), but i decided to try to make the case less specific to try to see where it fails.

I think aarg figured it out. He is spot on. It is defaulting to the else statement and flipping my flag. Thankyou aarg. I need to introduce a special case to break.

gfvalvo your suggestion is dually noted. I will post to a repo next time. My justification was to keep my pirate classmates away from my code that offer no help. I understand your point of view as well

Please post a COMPLETE program that people can drop into the IDE, run, and see what you're seeing. If your code is large, messing, and full of unrelated clutter, post an MRE.

megalomaniacgt:

 else if(stringed[COUNT] == '\0' )

{
             Serial.print(" DEBUG - Enters else if case");
             stringed[COUNT+1] = toUpperCase(stringed[COUNT+1]);
             COUNT++;
       }

What is the function toUpperCase() ? There is a library function for c strings called toupper().

dougp:
What is the function toUpperCase() ? There is a library function for c strings called toupper().

We are utilizing the Energia functions in our current coursework while using the msp432 board

https://energia.nu/guide/tutorials/strings/string_casechanges/

I should have noted that in the original post.

Ok i got it all resolved.

aarg was correct. It was defualting to the else statement when it wasn't a space. Also instead of checking for a null I should have checked for a space being stored.

Replacing the else if and else with an straight up if statement solved it and checking for the correct char

// Uppercase each word
      if(UPPERFLAG == 1)
      {
        if(COUNT == 0)
        {
          stringed[COUNT] = toUpperCase(stringed[COUNT]);
        }
        if(stringed[COUNT] == ' ' && (isAlpha(stringed[COUNT+1]) != 0 ))
        {
         stringed[COUNT+1] = toUpperCase(stringed[COUNT+1]);
          COUNT++;
        }
        COUNT++;
        if(stringed[COUNT] == '\r')
        {
          Serial.print("\nWord Count: ");
          Serial.print(WCOUNT);
          Serial.print("\nVowel Count: ");
          Serial.print(VCOUNT);
          Serial.print("\n");
          Serial.print(stringed);
          Serial.print("\n");
          UPPERFLAG = 0;
          COUNT = 0;
          Display();
          VCOUNT = 0;
          WCOUNT = 0;
        }
        
      }

I cant express enough gratitude to the contributing members, Ill smash the karma button for you aarg