| | |
Return Length of a string?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2003
Posts: 6
Reputation:
Solved Threads: 0
Here's my code. I just want to return the length of a string. How do I do that? Any assistance would be greatly appreciated.
I run this and get a slew of "Cannot convert..." errors.
C++ Syntax (Toggle Plain Text)
#include "stdafx.h" #using <mscorlib.dll> using namespace System; int _tmain() { String *strString; int intLength; Console::Write(S"Enter a String "); strString = Console::ReadLine(); Console::Write(strString); intLength = strlen(strString); Console::Write(intLength); return 0; }
I run this and get a slew of "Cannot convert..." errors.
•
•
Join Date: Sep 2003
Posts: 81
Reputation:
Solved Threads: 0
Can't you include string.h ? Use stdlib.h sizeof() and then search for '\0' (string end symb). You can make it even simplier by memsetting your string to some char (for example '\n') and then testing for that..
Ilya
P.S.: (You can later convert all the free mem to '\0' so that your print keeps clean)
P.P.S.: If all else fails instead of char symb. use hexidecimals (the true form of special chars)
Ilya
P.S.: (You can later convert all the free mem to '\0' so that your print keeps clean)
P.P.S.: If all else fails instead of char symb. use hexidecimals (the true form of special chars)
•
•
•
•
Originally Posted by Daywalker46410
Here's my code. I just want to return the length of a string. How do I do that? Any assistance would be greatly appreciated.
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
int _tmain()
{
String *strString;
int intLength;
Console::Write(S"Enter a String ");
strString = Console::ReadLine();
Console::Write(strString);
intLength = strlen(strString);
Console::Write(intLength);
return 0;
}
I run this and get a slew of "Cannot convert..." errors.
•
•
Join Date: Sep 2003
Posts: 81
Reputation:
Solved Threads: 0
ok..
here is something i didn't test:
here is something i didn't test:
C++ Syntax (Toggle Plain Text)
int strings(char *string) { int size=0; for (;string[size]!='\0'; size++) continue; return size; }
•
•
Join Date: Sep 2003
Posts: 81
Reputation:
Solved Threads: 0
Your problem might be that strlen returns size_t or smthn so and you are using int.. although I always thought that size_t was made by "typedef size_t int;"
Ilya
Ilya
•
•
•
•
Originally Posted by Daywalker46410
Here's my code. I just want to return the length of a string. How do I do that? Any assistance would be greatly appreciated.
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
int _tmain()
{
String *strString;
int intLength;
Console::Write(S"Enter a String ");
strString = Console::ReadLine();
Console::Write(strString);
intLength = strlen(strString);
Console::Write(intLength);
return 0;
}
I run this and get a slew of "Cannot convert..." errors.
•
•
Join Date: Sep 2003
Posts: 22
Reputation:
Solved Threads: 0
How bout' something like this:
Theoretically, that should give you the length of a string. Might not work since I thought it up right now.
I didn't know there was a system class. I r teh n00b.
Apparently, that class doesn't like you using 'sizeof()' on it, so you're just going to have to manually find it's size.
You are using C/C++, right? If so, I didn't know you could do that.
C++ Syntax (Toggle Plain Text)
int length(char *string) { int blah = 0; char *pointer = string; while (*pointer++!=NULL) blah++; return blah; }
Theoretically, that should give you the length of a string. Might not work since I thought it up right now.
•
•
•
•
When I use sizeof(strString) I get an error that says "Cannot apply sizeof to managed type 'System:tring'."
Apparently, that class doesn't like you using 'sizeof()' on it, so you're just going to have to manually find it's size.
•
•
•
•
#using <mscorlib.dll>
![]() |
Similar Threads
- Zero-Length-String (C++)
- Return last token in a string (C)
- spliting a string in vb6 (Visual Basic 4 / 5 / 6)
- how to find length of a string (C++)
Other Threads in the C++ Forum
- Previous Thread: stuck with definition
- Next Thread: inheritance
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






tring'."