Counting strings

i ant to count characters in a string without using the strlen functio

Page 1 of 1

5 Replies - 1034 Views - Last Post: 03 December 2009 - 05:12 PM Rate Topic: -----

#1 urikorulzs2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-December 09

Counting strings

Post icon  Posted 03 December 2009 - 12:26 PM

Ok like i said i want to count the number of characters in a string, without using the strlen function.
Heres what i have figured out so far, i just need help witth the rest of it.

 

int mystrlen(char *s)
{
	char s[] = ”program”;
	int i;
	for (i = 0; s[i] != 0; i++)

	printf( "The number of characters is %d", i);

	return i;
}




Mod edit: fixed code tags : :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Counting strings

#2 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,403
  • Joined: 13-June 08

Re: Counting strings

Posted 03 December 2009 - 12:32 PM

Don't you mean '\0'?

On another note, i'm not sure strings are null terminated.

Also, can't you use begin/end?

This post has been edited by ccubed: 03 December 2009 - 12:35 PM

Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: Counting strings

Posted 03 December 2009 - 02:28 PM

C style strings are and based on your parameters I imagine you aren't using std::string

int strSize(char* str){
	int length = 0;
	while(*str != '\0'){ //the end of a C style string is the null terminator
		length++; 
		str++; //increment the pointer, its base is str[0] str++ = str[1], so on and so forth
	}
	return length;
}


int main(){
	printf("%d\n", strSize("Knowles")); //7
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,403
  • Joined: 13-June 08

Re: Counting strings

Posted 03 December 2009 - 03:19 PM

So STD::String is not null terminated?
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Counting strings

Posted 03 December 2009 - 03:35 PM

I went looking in the debugger (gdb) and found this:
(gdb) p myString
$2 = {
  static npos = 18446744073709551615, 
  _M_dataplus = {
	<std::allocator<char>> = {
	  <__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, 
	members of std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Alloc_hider: 
	_M_p = 0x100100098 "This is a string"
  }
}
(gdb) x/24bx 0x100100098
0x100100098:	0x54	0x68	0x69	0x73	0x20	0x69	0x73	0x20
0x1001000a0:	0x61	0x20	0x73	0x74	0x72	0x69	0x6e	0x67
0x1001000a8:	0x00	0x00	0x00	0x00	0x00	0x00	0x00	0x00
(gdb) 



Looks null-terminated, but that could be an artifact of the debug compilation.
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Counting strings

Posted 03 December 2009 - 05:12 PM

or this:
#include <stdio.h>

unsigned long strlength(const char *str) {
	int i = 0;
	while(str[i++]) { }
	return i - 1; //remember the ++ adds an extra 1.
}

int main() {
	char countMe[] = "I had a tiny turtle his name was Tiny Tim.";
	int len = strlength(countMe);
	printf("\"%s\" has %d characters", countMe, len);
	return 0;
}


reads a little better...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1