home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

strlen() C++

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2167
Discussions: 200,947, Posts: 2,379,379, Members: 246,307
Old March 28th, 2005, 10:09 AM   Digg it!   #1 (permalink)
Member
 
redfoxstorm's Avatar
 
Join Date: Feb 2005
Posts: 386
Question
strlen() C++

I've got to write a program that simulates strlen in C++. I can't use any standard method or call up a function from string.h or anything like that.

What i really need is for someone to point me to some information on how strlen works. I don't want help with the code, just an idea of how strlen works.


once i know how strlen works, i can write the program that will take its place.

RedFox
__________________
For the Emperor, there were the Jedi. For the Evil Empire of Microsoft, there is a penguin.
redfoxstorm is offline   Reply With Quote
Old March 28th, 2005, 10:49 AM     #2 (permalink)
Perfetc Member
 
VHockey86's Avatar
 
Join Date: Jan 2003
Location: Maryland Suburbia
Posts: 4,334
I'm assuming it just iterates through the character array until it reaches the end... incrementing a counter on each iteration. Not totally sure although because I normally just use java.

Does every array contain a "size" data member in C++?
I know in java you could simply say "myCharArray.size" if you had a character array instead of the regular string class.
VHockey86 is offline   Reply With Quote
Old March 28th, 2005, 11:07 AM     #3 (permalink)
Member
 
redfoxstorm's Avatar
 
Join Date: Feb 2005
Posts: 386
Not sure.... now i think i'm confused on whats the difference between an array and a string.
redfoxstorm is offline   Reply With Quote
Old March 28th, 2005, 12:27 PM     #4 (permalink)
Member
 
redfoxstorm's Avatar
 
Join Date: Feb 2005
Posts: 386
Post

Quote:
#include <iostream>
using namespace std;

int stringlength(char[]);


//Main That does all the printing
int main(){

char string[]={"I'm a String in Jacob's Console"};
int total;



total= stringlength(string);

cout << "The Number of Charaters in.. " <<endl;
cout << string <<endl;
cout << "is: " << total << endl;

system("PAUSE");
return 0;

}

//Function that does all the work of strlen()
int stringlength(char string[]){

int total;
total = 0;


for (int i=0;i<100; i++){

if (string[i] != '\0'){

total++;
}
else{
break;
}

}
return total;
}


Now, that i have wrote the program, who do I accept user input for the string? I declared the string in this program. So, how can i use the "cin" to get the user defined string that I want to pass threw my function?

any ideas?
redfoxstorm is offline   Reply With Quote
Old March 28th, 2005, 01:36 PM     #5 (permalink)
Perfetc Member
 
VHockey86's Avatar
 
Join Date: Jan 2003
Location: Maryland Suburbia
Posts: 4,334
Why are you using a "for loop" from 0 to 100? Hard coding like that is going to restrict the user from getting accurate results with character arrays any longer than that.

Just use your "if" condition in a "while" loop.

Code:
int total = 0 ;

while(string[i] != '\0') {
     total ++;
}
If something is syntactically incorrect I appologize as I don't know C++ =(.

As for the latter question... I'm not sure how C++ I/O streams work. Is there any kind of "readLine" buffered functionality so that you can get an array from the input stream...

Otherwise the only thing I can think of is a dynamic array, and keep adding in characters from the stream.

---edit--
After a quick google I found something that ought to work for you
http://www.cs.hmc.edu/~geoff/classes.../notes/io.html

" cin.getline(char *buffer, int length)
Reads characters into the string buffer, stopping when (a) it has read length-1 characters or (b) when it finds an end-of-line character ('\n') or the end of the file. Stores a null character ('\0') after the last character read."

Last edited by VHockey86 : March 28th, 2005 at 01:41 PM.
VHockey86 is offline   Reply With Quote
Old March 28th, 2005, 07:50 PM     #6 (permalink)
I am a banana!
 
originel's Avatar
 
Join Date: Jun 2002
Location: Texas Tech
Posts: 3,921
Send a message via AIM to originel
Vhokey is correct (both syntatically and in how it works ). That second method uses the standard library though, so he can't use it.

As to get the string using cin, make a call to getline like VHockey said. you will want to use the size of your character array for the length in getline.

A string is just an array of characters so they are one and the same.

As for the java thing, c++ doesn't use classes inherently like java does and so arrays don't have any class members.
originel is offline   Reply With Quote
Old March 28th, 2005, 08:26 PM     #7 (permalink)
Member
 
redfoxstorm's Avatar
 
Join Date: Feb 2005
Posts: 386
okay, we haven't made it to the "getline" just yet. I might just try turning it in after i clean the code up a little bit(take that for statement out). He never said have the user input the string, but I was just wanting to go the extra mile.

is "getline" a C++ pointer?'

oh yeah, and thanks again to originel and big thanks to VHockey86!!! you guys saved a newbie today!

redfox
redfoxstorm is offline   Reply With Quote
Old March 28th, 2005, 09:33 PM     #8 (permalink)
I am a banana!
 
originel's Avatar
 
Join Date: Jun 2002
Location: Texas Tech
Posts: 3,921
Send a message via AIM to originel
getline is a function. do:

cin.getline(mystring,sizeofmystring);
originel is offline   Reply With Quote
Old March 28th, 2005, 10:06 PM     #9 (permalink)
Member
 
redfoxstorm's Avatar
 
Join Date: Feb 2005
Posts: 386
can't do any other function in the program besides the one we come up with for called stringlength.

but its always good to know ! Thanks! I might try it, see what my teacher thinks
redfoxstorm is offline   Reply With Quote
Old March 28th, 2005, 10:13 PM     #10 (permalink)
I am a banana!
 
originel's Avatar
 
Join Date: Jun 2002
Location: Texas Tech
Posts: 3,921
Send a message via AIM to originel
can you still do cin and cout? if so , you can do:
Code:
char l = 'a', char string[size];
int i = 0;
while(l != 0x0D)
{
     cin>>l;
     string[i] = l;
     i++;
]
string[i] = NULL
i would also be your length

EIDT: I was thinking premade strings. 0x0D is one of the two characters (along with 0x0A that comprises a carriage return (i.e. the user hitting enter). Also forgot about the terminating character.

Last edited by originel : March 29th, 2005 at 12:11 AM.
originel is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Making Health Care Worse (167)
The disrespect of Obama by Russian .. (19)
Is It Just Me? (2933)
Wireless Televisions. (11)
windows 7 problem (7)
CPU fan stops spinning randomly (8)
Regular Build (6)
Is the PSU I received dead? (12)
radeon x850xt platinum & shader.. (5)
Print spooler problem (15)
HIS HD5770 graphic card question (15)
windows vista security holes (9)
Install XP pro and a Vista laptop ?.. (11)
Foreign voltage (10)
Recent Discussions
Internet Lost (0)
Graphics Card Upgrade Questions (0)
Graphics Card Upgrade Question (2)
Asus P4G8X Mobo (6)
radeon x850xt platinum & shader 3 (5)
Xbox 360 GTA: SA disk error (1)
Is the PSU I received dead? (12)
How to convert MP3's (2)
windows 7 internet problem (5)
Multiple Restarts Required at Boot (0)
BSOD On Startup (ntoskrnl.exe) (2)
Print spooler problem (15)
Laptop with wireless problem. (1)
Wireless Televisions. (11)
Have you switched yet? (86)
screen resolution vs monitor size (2)
sms storage to PC (0)
Regular Build (6)
Open With ..... Win7 (0)
java code for fibonacci (1)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (35)
windows 7 problem (7)
CPU fan stops spinning randomly (8)
Partition Magic caused HDD problem (3)
Point and Shoot Camera Suggestions. (2)


All times are GMT -4. The time now is 07:33 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



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