Jump to content
xisto Community
xboxrulz1405241485

CharAt For C?

Recommended Posts

Hey guys,
I'm just wondering if there's a CharAt function like there is in Java?

I know in Java, you just do this:

intArray[0][intCount] = strSIN.charAt(intCount) - '0';

Thanks,
xboxrulz
I'm not sure what type did you declare strSIN as. As far as I know, there's no base string type available in C++ (well, I've not touch C++ for a few years now, maybe something new popup). String normally are represented as char[] or *char or CString. There are other representation, but are less popular. For char[] and *char, it should be easy as it's an array, "CharAt" would simply means char[at] (I forgotten how to do it with *char, but you can lookup in google).

As for CString, you need to refer to your class reference, since it could be from ATL, MFC or other lib, they all have different implementation.

EDIT: PS: I'm refering to windows here. If you're talking about linux, then CString might not applies
Edited by faulty.lee (see edit history)

Share this post


Link to post
Share on other sites

You may try this to see if it it help

 

for char*

int index = 0;char *message = "Hello, Java";char achar = *(message + index);orchar achar2 = message[index];

If I'am not forgot it, there is a class called string on STL.

But I'am not good with this class.

Edited by magiccode9 (see edit history)

Share this post


Link to post
Share on other sites

Sorry, I haven't see this ;)

 

Hi, for c++ string reference.

Here have a great web site.

 

[qutoe]

http://www.cplusplus.com/reference/string/string/

 

It should not hard because it looks like java and c# string

 

-----------------------

For quick way

because the message pointer variable was 0-based index array

So, please change the index variable to point to the char you need.

then loop throught it, as,

 

#include "stdio.h"#include "string.h"int main(){	// change the index to start the looping	int index = 2;	// change this to the number of char required (+ 1 if including null char)	int numrequiredchars = 8;	// build a simple buffer to hold a new data	char *requiredchars = new char[numrequiredchars];	// the source text	char *message = "Hello, Java and long long text";	memset(requiredchars, 0, numrequiredchars);	for (int i = index; i < numrequiredchars; i++)	{	   *requiredchars = *(message + i);		 ++requiredchars;	}	printf("%s\n", requiredchars);	//printf("\n%s\n", message);}
---

For relatively full story

 

 

I'am not good in explaination but I try my best,

 

char *message = "Hello, Java"  means a char pointer variable  that is pointing to a memory location in system that start with char H and contain a string Hello, Java

Because char *message = "Hello, Java" basically equal char[] = "Hello, Java".

So both of it are an char array, except that the formal including a null char at the end of char array ( the string )

And since a char is occupy 1 byte memory, so a pointer address = 1 byte, i.e.

 

0001 => H0002 => e

compared with an integer type that is 4 byte in size, i.e.

 

0001 => 10005 => 2

That result in,

*message = H*(message + 1) = e*(message + 2) = l...

If you just print out the message variable with printf,

it will show the address that it contains.

To get the actually value it pointed to,

use the * operator to dereference, as,

*message

 

 

Since not very good in English.

So it might be something missed.

 

Wish this help !

Edited by magiccode9 (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.