Return Length of a string?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2003
Posts: 6
Reputation: Daywalker46410 is an unknown quantity at this point 
Solved Threads: 0
Daywalker46410 Daywalker46410 is offline Offline
Newbie Poster

Return Length of a string?

 
0
  #1
Sep 29th, 2003
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.

  1. #include "stdafx.h"
  2.  
  3. #using <mscorlib.dll>
  4.  
  5. using namespace System;
  6.  
  7. int _tmain()
  8. {
  9.  
  10. String *strString;
  11. int intLength;
  12.  
  13. Console::Write(S"Enter a String ");
  14. strString = Console::ReadLine();
  15. Console::Write(strString);
  16. intLength = strlen(strString);
  17. Console::Write(intLength);
  18.  
  19. return 0;
  20. }

I run this and get a slew of "Cannot convert..." errors.
Reply With Quote Quick reply to this message  
Join Date: Sep 2003
Posts: 81
Reputation: Valmian is an unknown quantity at this point 
Solved Threads: 0
Valmian Valmian is offline Offline
Junior Poster in Training

Re: Return Length of a string?

 
0
  #2
Sep 30th, 2003
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)


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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2003
Posts: 81
Reputation: Valmian is an unknown quantity at this point 
Solved Threads: 0
Valmian Valmian is offline Offline
Junior Poster in Training

Re: Return Length of a string?

 
0
  #3
Sep 30th, 2003
Although I must say your problem is strange...
Reply With Quote Quick reply to this message  
Join Date: Sep 2003
Posts: 6
Reputation: Daywalker46410 is an unknown quantity at this point 
Solved Threads: 0
Daywalker46410 Daywalker46410 is offline Offline
Newbie Poster

Re: Return Length of a string?

 
0
  #4
Sep 30th, 2003
When I use sizeof(strString) I get an error that says "Cannot apply sizeof to managed type 'System:tring'."
Reply With Quote Quick reply to this message  
Join Date: Sep 2003
Posts: 81
Reputation: Valmian is an unknown quantity at this point 
Solved Threads: 0
Valmian Valmian is offline Offline
Junior Poster in Training

Re: Return Length of a string?

 
0
  #5
Sep 30th, 2003
ok..
here is something i didn't test:

  1. int strings(char *string)
  2. {
  3. int size=0;
  4. for (;string[size]!='\0'; size++)
  5. continue;
  6. return size;
  7. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2003
Posts: 81
Reputation: Valmian is an unknown quantity at this point 
Solved Threads: 0
Valmian Valmian is offline Offline
Junior Poster in Training

Re: Return Length of a string?

 
0
  #6
Sep 30th, 2003
ok.. I tested it and to my suprise (my stuf ususally doesn't work from the first try) it actually worked.. so I hope you won't have problems with it.
Ilyaa
Reply With Quote Quick reply to this message  
Join Date: Sep 2003
Posts: 81
Reputation: Valmian is an unknown quantity at this point 
Solved Threads: 0
Valmian Valmian is offline Offline
Junior Poster in Training

Re: Return Length of a string?

 
0
  #7
Sep 30th, 2003
*ilya
Reply With Quote Quick reply to this message  
Join Date: Sep 2003
Posts: 81
Reputation: Valmian is an unknown quantity at this point 
Solved Threads: 0
Valmian Valmian is offline Offline
Junior Poster in Training

Re: Return Length of a string?

 
0
  #8
Sep 30th, 2003
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

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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 129
Reputation: Bob is an unknown quantity at this point 
Solved Threads: 1
Team Colleague
Bob Bob is offline Offline
Team Member

Re: Return Length of a string?

 
0
  #9
Oct 4th, 2003
Doesn't look like C++ to me, is it C#?
Reply With Quote Quick reply to this message  
Join Date: Sep 2003
Posts: 22
Reputation: Mike29936 is an unknown quantity at this point 
Solved Threads: 0
Mike29936 Mike29936 is offline Offline
Newbie Poster

Re: Return Length of a string?

 
0
  #10
Oct 7th, 2003
How bout' something like this:

  1. int length(char *string) {
  2. int blah = 0;
  3. char *pointer = string;
  4. while (*pointer++!=NULL) blah++;
  5. return blah;
  6. }

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'."
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.

#using <mscorlib.dll>
You are using C/C++, right? If so, I didn't know you could do that.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC