Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,489 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,290 people online right now. Registration is fast and FREE... Join Now!




Anything similar to charAt() in C?

 
Reply to this topicStart new topic

Anything similar to charAt() in C?, Finding characters

martina
22 Mar, 2007 - 09:11 AM
Post #1

New D.I.C Head
*

Joined: 13 Mar, 2007
Posts: 7


My Contributions
Is there anything similar to Java's charAt[i] function in C?

I need to find the third element of each word in an array of strings, eg {"catfish", "doggie", "random"}, where the strings are some random words.

How do I get the third element of the second word for example if I don't know what the second word actually is?
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Anything Similar To CharAt() In C?
22 Mar, 2007 - 09:31 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,867



Thanked: 53 times
Dream Kudos: 550
My Contributions
CODE

#include <iostream>
using namespace std;
...

string str = "Hello World";
cout << str[0] << " " << str[6]; //should give "H W"


Well that works in C++, in C strings are character arrays so:

CODE

char str[] = "Hello World";
printf("%c", str[0]);

User is online!Profile CardPM
+Quote Post

martina
RE: Anything Similar To CharAt() In C?
22 Mar, 2007 - 09:48 AM
Post #3

New D.I.C Head
*

Joined: 13 Mar, 2007
Posts: 7


My Contributions
Thank you, NickDMax!

Yes, that's right, you can find a character in a single string this way, but when you have a pointer array, eg:

CODE
char** an_array_of_strings[3]={"balloon", "whatever", "isnext"};


-- how do you find out which is the third character of the second word? In "whatever" it is an 'a' but what if you don't know the words?
I've learned java before but now I'm a bit confused with pointers in C.

I just think if I should save each word in a different array first and then find the third character of each word or there is any better way to do that.
With pointers anyhow?

(I just don't understand these pointers very well yet.)
User is offlineProfile CardPM
+Quote Post

horace
RE: Anything Similar To CharAt() In C?
22 Mar, 2007 - 10:02 AM
Post #4

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
you can access the third character of the second word so
CODE

    char* an_array_of_strings[]={"balloon", "whatever", "isnext"};
    cout << an_array_of_strings[1][2] << endl;

remember the strings "balloon" etc are constants

If you need to change the strings use
CODE

    char an_array_of_strings[][20]={"balloon", "whatever", "isnext"};
    cout << an_array_of_strings[1][2] << endl;

string are a maximum 20 characters long including '\0' terminator
User is offlineProfile CardPM
+Quote Post

martina
RE: Anything Similar To CharAt() In C?
22 Mar, 2007 - 10:16 AM
Post #5

New D.I.C Head
*

Joined: 13 Mar, 2007
Posts: 7


My Contributions
Thank you, Horace!

Think I got it. Just need to use a 2D array!

BTW, do you know any good site about pointers in C?
(Something like "Pointers for Dummies" perhaps? biggrin.gif biggrin.gif )
I really would like to understand how to use them.

I'm a total beginner in C and those pointers are confusing... Oy! crazy.gif

User is offlineProfile CardPM
+Quote Post

horace
RE: Anything Similar To CharAt() In C?
22 Mar, 2007 - 11:12 AM
Post #6

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
QUOTE(martina @ 22 Mar, 2007 - 06:16 PM) *

Thank you, Horace!

Think I got it. Just need to use a 2D array!

BTW, do you know any good site about pointers in C?
(Something like "Pointers for Dummies" perhaps? biggrin.gif biggrin.gif )
I really would like to understand how to use them.

I'm a total beginner in C and those pointers are confusing... Oy! crazy.gif

have a look at
http://www.cplusplus.com/doc/tutorial/pointers.html

User is offlineProfile CardPM
+Quote Post

martina
RE: Anything Similar To CharAt() In C?
22 Mar, 2007 - 11:26 AM
Post #7

New D.I.C Head
*

Joined: 13 Mar, 2007
Posts: 7


My Contributions
Tnx a lot!!!!

Woohoooo!!! I got my program to work with these 2D arrays!!!!
I'm sooooo happppyyyyy!!!

You are a genius, Horace, do you know that??? biggrin.gif

icon_up.gif icon_up.gif icon_up.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 05:23PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month