#How can i write a program that takes input and shows the number of characters and spaces?
19 messages · Page 1 of 1 (latest)
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.
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.
i havent learned that function yet but i guess ill just have to look it up then n see how it work
what they mean is that your input is likel a c-string, hence a char* pointer that has the extra property to be null-terminated: there is a special character '\0' at the end of your data, that represents the end of it.
Consider the following example, as an example:
;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);
}
Length of "Hello, world!" is 13
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
@heavy dove still confused?
I'm still trying to understand how this break sequence works...what does c == '\0' mean?
Sorry yeah
It compares the character in the variable c with the character constant '\0'. This character is used to mark the end of a string (character array). in C "abc" will be the characters 'a', 'b' and 'c' plus one more character, a '\0' which is just an 8 bit character value with all bits set to zero. It is used to mark the end of the string.
Basically, it breaks the loop at the end of the string.
Ohh that makes a little more sense
Thanks
I'll try to do it now
@heavy dove Has your question been resolved? If so, type !solved :)