#How can i write a program that takes input and shows the number of characters and spaces?

19 messages · Page 1 of 1 (latest)

heavy dove
#

ive been at it for a while now and still cant figure it out....i know its prolly going to use a loop function but i dunno how to impliment it....any tips would be helpful thanks

static fiberBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

heavy dove
#

also i can not use strlen() or arrays

#

TwT

wild gorge
#

char* is an address. You can iterate through each bit of the array it points to (and get the value) until you reach the null terminator.

heavy dove
formal rivet
#

;compile

#include <stdio.h>


int main(void) {
  char *message = "Hello, world!";

  int length = 0;

  int i = 0;
  for(;;) { /* loop forever. manual break below */
    char c = message[i];
    if(c == '\0') {
      // end of string, exit
      break;
    } else {
      length += 1; // increment the length otherwise
    }
    i += 1;
  }

  printf("Length of \"%s\" is %d\n", message, length);
}
little whaleBOT
#
Program Output
Length of "Hello, world!" is 13
formal rivet
#

now, what you want is not the length of the string, but the number of characters and the numbr of spaces. so if the code example above is clear, you could try to adapt it to compute two things

surreal hound
#

@heavy dove still confused?

heavy dove
heavy dove
safe flint
#

Basically, it breaks the loop at the end of the string.

heavy dove
#

Thanks

#

I'll try to do it now

static fiberBOT
#

@heavy dove Has your question been resolved? If so, type !solved :)