Page 1 of 1
VB: Mid(), Len() C++: ???, ??? please help
Posted: Mon Jun 23, 2003 5:38 pm
by Scarberry
u know the len and mid statements in vb well i was wondering what their equivlent (i cant spell) was in Win32 C++ and what libraries (#include <thing.h>) do i need to include to use them?
is it the same as in C? if not what r they in C?
thanks
Re:VB: Mid(), Len() C++: ???, ??? please help
Posted: Mon Jun 23, 2003 5:41 pm
by Tim
In C++, you have the std::string class.
For instance:
Code: Select all
#include <string>
std::string str;
str = "Hello";
cout << str.size(); // prints 5
I'm not sure whether std::string has a direct equivalent of Mid (although it's easy to obtain one character at a time).
Alternatively, you might want to look at MFC's CString.
None of these are available in C (not C++). If you want to use C strings, I recommend you read up on them in a book.
Re:VB: Mid(), Len() C++: ???, ??? please help
Posted: Mon Jun 23, 2003 6:41 pm
by slacker
it would proboly be easier to write a len or mid function....
Re:VB: Mid(), Len() C++: ???, ??? please help
Posted: Mon Jun 23, 2003 8:56 pm
by Scarberry
yes i did use that code u sent me and i did turn it into a len statement but u said it would be easy to get a single letter? could u show me how?
thanks
Re:VB: Mid(), Len() C++: ???, ??? please help
Posted: Tue Jun 24, 2003 1:43 am
by Tim
Strings in C and C++ are treated as arrays of characters. Use stringname[index].
Re:VB: Mid(), Len() C++: ???, ??? please help
Posted: Wed Jun 25, 2003 1:51 am
by Scarberry
AH HA!!! Thank you
just 2 more questions
---------------------------------------
how can i return the number of charcters in a string including spaces?
---------------------------------------
when loading a string from a file it returns words. i want to be able to return everything thats in the file or i could do it if there was a way to tell how many words are in the file?
thanks
Re:VB: Mid(), Len() C++: ???, ??? please help
Posted: Wed Jun 25, 2003 2:10 am
by Tim
Scarberry wrote:how can i return the number of charcters in a string including spaces?
As I said earlier, size() (if you're using std::string) or strlen() (if you're using C strings).
when loading a string from a file it returns words. i want to be able to return everything thats in the file or i could do it if there was a way to tell how many words are in the file?
fopen, fread, fclose will read bytes from a file. Keep calling fread until it fails to read any more.
Re:VB: Mid(), Len() C++: ???, ??? please help
Posted: Wed Jun 25, 2003 2:49 am
by scarberry
Quote from: Scarberry on Today at 02:51:32am how can i return the number of charcters in a string including spaces?
As I said earlier, size() (if you're using std::string) or strlen() (if you're using C strings).
umm it stops at spaces
e.g.
str="Hello How Are You";
cout<<str.size();
that would print out "5"
i want to be able to keep the spaces as well so it would print out a "17" instead of a "5"
thanks
Re:VB: Mid(), Len() C++: ???, ??? please help
Posted: Wed Jun 25, 2003 7:35 am
by slacker
Code: Select all
int count(string str)
{
int retval=0;
for(int i=0;str[i]!=0;i++)
retval++;
return retval;
}
something like that
Re:VB: Mid(), Len() C++: ???, ??? please help
Posted: Wed Jun 25, 2003 11:21 am
by Tim
scarberry wrote:umm it stops at spaces
No, it doesn't. Each std::string keeps track of the string length.
std::string.size() doesn't even stop at '\0' characters like strlen() does.