counting characters without strlen

i'm trying to write a program about measuring the length of a string without using strlen.
i have wrote something but my problem is i don't know how to add '\0' at the end of the writing!

here's my code:(it's so simple)

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
  #include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;

int main()
{
	char s[100];
	int C=0;

	cout<<"write something down:"<<endl;
	gets(s);

	

	for(int i=0;i<'\0';i++)
	{
		C++;
	}

	cout<<"your writing has "<<C<<" chatecters!"<<endl;

	getch();
	return 0;
}
you are first reading the string of characters in the variable 's'.

However, you are not using this variable at all in the for loop!

How will the loop variable 'i' know when to stop.

if you do not want to use the simple string library and call str.length() you can maybe try this:

1
2
3
int count = 0;
for (; s[count] != '\n'; count++) {}  // i'm not sure myself what the newline character should be. please check
cout << count;
Last edited on
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
#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;

int main()
{
	char s[100];
	int C=0;

	cout<<"write something down:"<<endl;
	gets(s);



	for(int i=0; s[i] != 0;i++)
	{
		C++;
	}

	cout<<"your writing has "<<C<<" chatecters!"<<endl;

	getch();
	return 0;
}
First, the user might write a one-liner longer than 99 characters and then you do have a problem. gets is not safe that way.

While, not for. Look at each s[i] until you find the one that equals '\0'.
@Pebble

your code will not count the number of zeroes entered by the user. (or did you mean '\0'?)
Last edited on
I don't have a clue if gets() adds the terminating character (\0) to the array, but getline does so as in:
 
cin.getline(s, 99);


Your for loop is not check if there is a \0 character in s. Instead, it is iterating until I is less than the ASCII value of \0, which happens to be 0. This means that your for loop shouldn't execute at all. Consider the following code:
1
2
3
4
5
int iter=0;
while(s[iter] != '\0' || iter < 100){
      C++;
      iter++;
}

This will iterate through the s array until it reaches a terminating character or the limit of the array.

I would like to point out that you are using more headers than you need (and the wrong ones too). iostream is the standard header for C++ projects. I will explain why you don't need the other two below:
- conio.h is a deprecated header whose functions do not act the same among different compilers. You may not experience this problem as many of the differences are minute (as with getch() ), but for your code to be more flexible, stick with the standard.
- stdio.h is actually a C file. The C++ version is cstdio. This is actually part of the standard, but mixing I/o methods is generally not a good idea unless you go through the trouble of syncing.

Your code roughly translated to C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
   char s[100];
   int C=0;
   
   cout << "Write something down: ";
   cin.getline(s, 99);

   for(int I = 0; s[I] != '\0', I < 100; I++)
      C++
 
   cout << "Your entry has " << C << " characters!" << endl;
   cin.ignore(10000,'\n');
   return 0;
}


Edit:
FML, I am such a slow typer. :(
Last edited on
It is funny. Not so far I read about an interview - programming test. The assignment was to write a function that reverses a character array without using standard C/C++ functions. So it was necessary to find the end of the character array before reversing the string that is to find the length of the string.
It is interesting to note that this assignment demonstrates the dualizm of C/C++. That to be clear what I am speaking about function strlen can be writeen in two ways .

The first way

1
2
3
4
5
6
7
8
std::ptrdiff_t strlen( const char *s )
{
   const char *p = s;

   while ( *p ) ++p;

   return ( p - s );
}



And the second way

1
2
3
4
5
6
7
8
std::size_t strlen( const char *s )
{
   std::size_t n = 0;

   while ( s[n] ) ++n;

   return ( n );
}



As I said the assignment was to write a reverse function for a character array without using standard C/;C++ functions.

I suggest you the same assignment but to write a recursive reverse function.:)
Last edited on
@ abhishekm71 It does count any 0 (zero) entered.
@abhishekm71

As the ascii code of '\0' is 0, testing a char against 0 is the same as testing it against '\0'.

Of course, some people (including me) prefer to use '\0' as it signals more clearly that a char is being tested.

@Daleth

string::getline deals with the null terminator for you, in that it allows space for it in the buffer length you pass to it. So you not need to pass one less than the buffer length. From example in site reference:
http://www.cplusplus.com/reference/istream/istream/getline/

1
2
3
4
  char name[256];

  std::cout << "Please, enter your name: ";
  std::cin.getline (name,256);


(256 used for array definition and in call)

But if you are being ultra security conscious, then you should ensure that your extra char is actually nulled (by zeroing the array).

Andy
Last edited on
ok, thans pebble and andy, it was new learning for me.
thank you all for helping!
Topic archived. No new replies allowed.