Count the " " Spaces (Char 32) in an array

******* UPDATE ********

see below I have a fix

Hi all is there a way to count the spaces ' ' or char 32, or nul text in an array,

"X X X" has 3 spaces

"XX " has 2 spaces

thanks Ben

count = 0;
for (uint8_t i=0; i<strlen(s); i++)
  if (s[i] == ' ') count++;

Is the data in the array null terminated? In other words, a variable length character string?

yes, its un terminated but a max of 6 char string

"un terminated"? Please clarify. Do you want to stop when you reach a '\0'?

it is max 6 char long, i want a count of the total " " (char 32) in the string

in all cases they are together, but it can be 1, 2, and a max of 4 " " 'spaces'

OR

need to remove the spaces (1, 2, 3 or 4 spaces only) after the lest real char, can be a letter or Number

The reply at #1 will tell you how many spaces are in your set of 6 characters.

What do you want to replace the spaces with?

markd833:
The reply at #1 will tell you how many spaces are in your set of 6 characters.

What do you want to replace the spaces with?

Hi Mark I cant get No.1 to work (honestly don't know where to start)

when I add it I keep getting not declared or other issues

if I know how many spaces I want to do an if else table

ie if 1 space do something, if 2 do something else ...........

void onIfeiFuelDownChange(char* newValue) {
  nextion.print("t9.txt=\"");
  nextion.print(newValue);
  nextion.print("\"");
  nextion.write("\xFF\xFF\xFF");
}
DcsBios::StringBuffer<6> ifeiFuelDownBuffer(0x7484, onIfeiFuelDownChange);

I cant get No.1 to work (honestly don't know where to start)

Post what you tried and the error messages that you got

The variable 's' at #1 needs to be replaced with the name of your variable that is holding the 6 characters.

We need to see your exact code, otherwise we're all just guessing.

I suspect an XY problem there, what are you really trying to solve for? Where does the string come from, what is generating those spaces?

All

I have a fix that is working

the below finds the first space (32) and from there I can add the spaces I need to the front of the text in the nextion.print, this now right just. the text in the nextion for me

ir if the input is

1234XX postion [4] adds 2 x Spaces moving the text to XX1234

//################# FUEL UPPER ##################
void onIfeiFuelUpChange(char* newValue) {
  if (newValue[2] == 32) {
    nextion.print("t8.txt=\"    ");
    nextion.print(newValue);
    nextion.print("\"");
    nextion.write("\xFF\xFF\xFF");
  }
    else if (newValue[3] == 32) {
    nextion.print("t8.txt=\"   ");
    nextion.print(newValue);
    nextion.print("\"");
    nextion.write("\xFF\xFF\xFF");
  }
  else if (newValue[4] == 32) {
    nextion.print("t8.txt=\"  ");
    nextion.print(newValue);
    nextion.print("\"");
    nextion.write("\xFF\xFF\xFF");
  }
  else if (newValue[5] == 32) {
    nextion.print("t8.txt=\" ");
    nextion.print(newValue);
    nextion.print("\"");
    nextion.write("\xFF\xFF\xFF");
  }
  else {
    nextion.print("t8.txt=\"");
    nextion.print(newValue);
    nextion.print("\"");
    nextion.write("\xFF\xFF\xFF");
  }
}
DcsBios::StringBuffer<6> ifeiFuelUpBuffer(0x748a, onIfeiFuelUpChange);

That’s probably suboptimal but if that’s good for you then that’s ok

I’d investigate what is calling onIfeiFuelUpChange() and how is that data string built in the first place.

J-M-L:
That’s probably suboptimal but if that’s good for you then that’s ok

I’d investigate what is calling onIfeiFuelUpChange() and how is that data string built in the first place.

yes it is from DCS Bios

DCS is the flight simulator, the Bios connects the PC to an external Simulator "Simpit"

changing DCS and DCS Bios will effect the other 10,000's players around the woruld, so we have to accommodate minor bugs for our own simulator and interpretation of the data

J-M-L:
That’s probably suboptimal but if that’s good for you then that’s ok

I’d investigate what is calling onIfeiFuelUpChange() and how is that data string built in the first place.

and yes the "onIfeiFuelUpChange" is the upper fuel gauge on the IEFI (f-18-integrated-fuel-engine-indicator)

IEFI.jpg

The image
IEFI.jpg

Ok so there is no other way to get the actual number than this weirdly formatted string ? Is the function called automatically as a call back or there is some sort of serial data acquisition/parser Stage before that ? Extracting useable information early might help.

You really should keep all this in one thread, this is the third topic you have started for the same problem. The previous post had only asked about including any text after the number, had not gotten an opportunity to ask if there needed to be leading spaces because you did not reply in that thread. Also never got a response on whether or not the code I posted worked - if you needed leading spaces it would not, but can be easily modified to add those.

Is there going to be any time that there will be a space within the text, or will spaces always be at either the beginning or end?

J-M-L:
The image
IEFI.jpg

Ok so there is no other way to get the actual number than this weirdly formatted string ? Is the function called automatically as a call back or there is some sort of serial data acquisition/parser Stage before that ? Extracting useable information early might help.

its done inside the PC between the DCS game and "DCS-Bios" there is no other way I know how, happy now as the issues apears for now working thanks

Ok