Nov 22, 2009

CharAt For C?

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > C, C++ & Visual C++

CharAt For C?

xboxrulz
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:
CODE
intArray[0][intCount] = strSIN.charAt(intCount) - '0';


Thanks,
xboxrulz

Comment/Reply (w/o sign-up)

faulty.lee
QUOTE(xboxrulz @ Sep 15 2008, 08:05 AM) *
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:
CODE
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

 

 

 


Comment/Reply (w/o sign-up)

magiccode9
You may try this to see if it it help

for char*
CODE
int index = 0;
char *message = "Hello, Java";
char achar = *(message + index);

or

char 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.

Comment/Reply (w/o sign-up)

xboxrulz
well, let me clarify that I use the string to store the SIN (Social Insurance Number) and I want to pick out the certain number out to analyze it.

Many thanks,
xboxrulz

Comment/Reply (w/o sign-up)

magiccode9
Sorry, I haven't see this sad.gif

Hi, for c++ string reference.
Here have a great web site.

[qutoe]
http://www.cplusplus.com/reference/string/string/
[/quote]

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,

CODE
#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,

CODE
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.

CODE
0001 => H
0002 => e


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

CODE
0001 => 1
0005 => 2


That result in,
CODE
*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 !

Comment/Reply (w/o sign-up)

xboxrulz
Alright, I found an alternative.

I'll keep this for reference.

Many thanks,
Adrien

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Charat


    Looking for charat, c,

See Also,

*SIMILAR VIDEOS*
Searching Video's for charat, c,
advertisement



CharAt For C?

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com