strlen() C++  | | |
March 28th, 2005, 10:09 AM
|
#1 (permalink)
| | Member
Join Date: Feb 2005
Posts: 386
| 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.
|
| |
March 28th, 2005, 10:49 AM
|
#2 (permalink)
| | Perfetc Member
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. |
| |
March 28th, 2005, 11:07 AM
|
#3 (permalink)
| | Member
Join Date: Feb 2005
Posts: 386
|
Not sure.... now i think i'm confused on whats the difference between an array and a string. |
| |
March 28th, 2005, 12:27 PM
|
#4 (permalink)
| | Member
Join Date: Feb 2005
Posts: 386
| 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? |
| |
March 28th, 2005, 01:36 PM
|
#5 (permalink)
| | Perfetc Member
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.
|
| |
March 28th, 2005, 07:50 PM
|
#6 (permalink)
| | I am a banana!
Join Date: Jun 2002 Location: Texas Tech
Posts: 3,921
|
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. |
| |
March 28th, 2005, 08:26 PM
|
#7 (permalink)
| | Member
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 |
| |
March 28th, 2005, 09:33 PM
|
#8 (permalink)
| | I am a banana!
Join Date: Jun 2002 Location: Texas Tech
Posts: 3,921
|
getline is a function. do:
cin.getline(mystring,sizeofmystring); |
| |
March 28th, 2005, 10:06 PM
|
#9 (permalink)
| | Member
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 |
| |
March 28th, 2005, 10:13 PM
|
#10 (permalink)
| | I am a banana!
Join Date: Jun 2002 Location: Texas Tech
Posts: 3,921
|
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.
|
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |