VB: Mid(), Len() C++: ???, ??? please help

Programming, for all ages and all languages.
Post Reply
Scarberry

VB: Mid(), Len() C++: ???, ??? please help

Post 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
Tim

Re:VB: Mid(), Len() C++: ???, ??? please help

Post 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.
slacker

Re:VB: Mid(), Len() C++: ???, ??? please help

Post by slacker »

it would proboly be easier to write a len or mid function....
Scarberry

Re:VB: Mid(), Len() C++: ???, ??? please help

Post 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
Tim

Re:VB: Mid(), Len() C++: ???, ??? please help

Post by Tim »

Strings in C and C++ are treated as arrays of characters. Use stringname[index].
Scarberry

Re:VB: Mid(), Len() C++: ???, ??? please help

Post 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
Tim

Re:VB: Mid(), Len() C++: ???, ??? please help

Post 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.
scarberry

Re:VB: Mid(), Len() C++: ???, ??? please help

Post 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
slacker

Re:VB: Mid(), Len() C++: ???, ??? please help

Post 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
Tim

Re:VB: Mid(), Len() C++: ???, ??? please help

Post 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.
Post Reply