Counting Characters?

Hello,
I am working on a program that counts the total number of characters in a string without using the strlen (string) function. When I run the program it just gives me some random number. I think the main body is fine there is just something wrong with the function that I created. This is my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#include <iostream>

using namespace std;

char StringLength (char c)
{
    char count[256];
    int size = 0;

    for ( c != 0; ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) ; c++ )
    {
    size += count[c];
    }
    return size;
}
int main()
{
    char c;
    int size = 0 ,length;
    cout << "Enter your string of characters: ";
    cin >> c;
    size = StringLength (c);
    cout << "The total number of characters entered is: " << size << endl;

    return 0;
}
Last edited on
char is just a single character (char is short for character).
The string class is called... string.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
using namespace std;

int main()
{
  string s;
  cout << "Enter your string: " << flush;
  getline(cin,s);
  const int size=s.length();
  cout << "The total number of characters entered is: " << size << endl;
}
I am trying to create a function that works like strlen (string) but I can't use #include <string>.
strlen() looks nothing like that.
1
2
3
4
5
int strlen(const char *a){
    const char *b;
    for (b=a;*b;++b);
    return b-a;
}
Either way, I don't think you can count a single character... You need to be counting character arrays, not single chars.

And... I understand you probably don't know the answer yourself, so don't worry about it, but... What on earth is the point of using C++, if you can't use strings, and you have to use char arrays and... yeah. You might as well use C with strlen().

If this is an assignment, I hate to say, but it's a bad one.

Course, I could be wrong.
Last edited on
Just in case I am not stating the problem right, here it is word for word:

The standard library function strlen() (found in string.h) returns the number
of characters in a string, not including the terminating NUL ('\0') character.
Write your own StringLength() function that does exactly the same thing.
Write a short main() program to declare and initialize a string. Pass the
string to StringLength() to demonstrate that it can return an integer value
representing the number of characters in the test string. Use a cout
statement in main() to print out the length of the string.

Please note: Your function should count the characters in the string, not
access some C++ library function or method. DO NOT #include <string.h> or
<string>.
Last edited on
Well, you're talking about C-style strings. C++ strings require the <string> standard header. You shouldn't say "string" when you mean "C string".

Basically, you could do what helios showed. A more beginner-friendly version would be this:

1
2
3
4
5
6
int my_strlen(const char* str)
{
    int n = 0;
    for(const char* p = str; p != 0; ++p, ++n);
    return n;
}

In your original version, you were only considering letters, thus excluding all other character, and the initialization part of your for loop wasn't really initializing anything. You were also adding character values to the size variable, rather than 1.
Last edited on
In case you aren't aware, in C++ a convention was established to terminate all strings with a null character (written as 0 or '\0'). Thus, to understand what filipe posted, you should realize that he is merely parsing the given string for this null/terminating character.
in C a convention was


Fixed.
My mistake! >.< Please forgive my stupidity. =[

Firedraco, would you mind bringing me up to date as far as how/why it changed?
I have tried to rewrite the code but it only returns 0. I'm not sure what is wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include <iostream>

using namespace std;

// Prototype declarations
int StringLength (const char*);

int main ()
{
    char MyName [] = "string";
    cout << "This string has " << StringLength (MyName) << " characters" << endl;

    return 0;
}

int StringLength (const char* MyName)
{
    int i = 0;
    for (const char* p = MyName; p != 0; ++p, ++i)
    return i;
}
Look at what your for is encompassing.
And look at your loop condition.
Topic archived. No new replies allowed.