calculate the length of the string

#include <iostream>
#include <conio>
#include <string>

int stringLength(char *);
int main()
{
const int SIZE = 100;
char sentence[SIZE];
int count;



cin.getline(sentence,SIZE);
cout << stringLength(sentence);


getch();
return 0;
}

int stringLength(char *stringLen)
{
char numcharacter;
numcharacter = strlen(stringLen);
return numcharacter;
}

This is the part of the program that could return the value OF HOW MANY CHARACTER OF THE STRING entered.But the problem is that the value of the length keep included the whitespace between character.How could i avoid it ,do i need to write it in loop form ?
You already have a function for that: strlen
http://www.cplusplus.com/reference/clibrary/cstring/strlen.html

If you want to make your own remember that the '\0' is the last for any C string
If you want to exclude whitespace, then you need to write your own strlen function. To get you started, here is an implementation of strlen:

1
2
3
4
5
6
7
8
size_t my_strlen( const char* str ) {
    size_t chars = 0;
    while( str && *str ) {
         ++chars;
        ++str;
    }
    return chars;
}


Now you can modify it to only count characters that are not whitespace.
Topic archived. No new replies allowed.