Calling C code monkeys
redhat
Sydney, New South Wales
524 posts
Some shiney props for anyone that can help me out.
I am sure someone will be able to do this simply.

I have a string that's 36 characters long and I need to return the nth character in a string. Have looked through all the functions starting with str* but none are helping me out.

I am assuming its a while loop, but the googles isn't helping me out either.
Send Private Message
12:20pm 29/10/09 Permalink
tequila
Brisbane, Queensland
3727 posts
lookup substr() instead
Send Private MessageWWW
12:23pm 29/10/09 Permalink
redhat
Sydney, New South Wales
525 posts
Will do, cheers Teq.
Send Private Message
12:27pm 29/10/09 Permalink
nF
Forum Hero
Wynnum, Queensland
16124 posts
stringname[n]
Send Private MessageWWW
12:44pm 29/10/09 Permalink
parabol
Brisbane, Queensland
5479 posts
stringname[n-1]

Fixed.

Though given the simplicity, the question is probably not what it seems.
Send Private Message
12:49pm 29/10/09 Permalink
Jim
Brisbane, Queensland
10610 posts
12:51pm 29/10/09 Permalink
nF
Forum Hero
Wynnum, Queensland
16125 posts
As if you wouldn't be counting from zero.
Send Private MessageWWW
12:51pm 29/10/09 Permalink
Scooter
Brisbane, Queensland
2096 posts
stringname[nF]

?
Send Private Message
12:59pm 29/10/09 Permalink
Pinky
Melbourne, Victoria
3047 posts
As if you wouldn't be counting from zero.

Oooh, nice save! Difficult to argue with this one.

BASIC anyone? :-P
Send Private Message
01:26pm 29/10/09 Permalink
redhat
Sydney, New South Wales
526 posts
Am counting from zero, :P

Worked it out thanks guys.
Send Private Message
01:45pm 29/10/09 Permalink
Obes
Brisbane, Queensland
8091 posts
Unless its apples you always count from 0
Send Private Message
01:51pm 29/10/09 Permalink
redhat
Sydney, New South Wales
527 posts
Say I have a few of these

stringname[3]
stringname[4]
stringname[5]

How do I get them back into a string by themselves? ie I want to be able to use %s in printf instead of using %c.

last edited by redhat at 16:33:56 29/Oct/09
Send Private Message
04:32pm 29/10/09 Permalink
Jim
Brisbane, Queensland
10613 posts
this is the first google result for 'strings in C'

http://www.macdonald.egate.net/CompSci/hstrings.html
Send Private Message
04:35pm 29/10/09 Permalink
Spook
Brisbane, Queensland
26748 posts
redhat is very bad at programming

u need ansi c by denis ritchie and some skillz
Send Private Message
04:37pm 29/10/09 Permalink
Opec
Brisbane, Queensland
5946 posts
either concat or copy.
Send Private Message
04:43pm 29/10/09 Permalink
Hogfather
Cairns, Queensland
3916 posts

Hahah hi Perlboy
Send Private MessageWWW
04:43pm 29/10/09 Permalink
redhat
Sydney, New South Wales
528 posts
Thanks guys.

Stupid engine written in C, WTB perl.
Send Private Message
04:51pm 29/10/09 Permalink
Hogfather
Cairns, Queensland
3918 posts
Stupid engine written in C, WTB perl
Hahah what the fuck ... first time this phrase has ever been uttered?!
Send Private MessageWWW
04:56pm 29/10/09 Permalink
Spook
Brisbane, Queensland
26750 posts
u know it hoggy!

lols, we got bought out by cpu a little while back, they love windows like no ones business and even they run all their jobs on perl

perl on windows, teehee
Send Private Message
04:57pm 29/10/09 Permalink
3dee
Brisbane, Queensland
4693 posts
char strichr (const char *str, int i)
{
     // easiest
    char c = str[i];

    // next best, not really worth it
    c = *(str + i);

    // over the top
    while (i--)
        str++;
    c = *str;

    return c;
}


last edited by 3dee at 17:14:41 29/Oct/09
Send Private MessageWWW
05:12pm 29/10/09 Permalink
mongie
Brisbane, Queensland
6785 posts
cpu?
Send Private MessageWWW
05:12pm 29/10/09 Permalink
redhat
Sydney, New South Wales
529 posts
perl is just easier because i've done it before :P

edit: ended up using sprintf!
Send Private Message
05:13pm 29/10/09 Permalink
Jim
Brisbane, Queensland
10617 posts
use snprintf
pretend sprintf doesn't exist
Send Private Message
05:18pm 29/10/09 Permalink
jay
Brisbane, Queensland
54 posts
you almost had us fooled thinking you were competent at your job ry.
Send Private Message
05:28pm 29/10/09 Permalink
Spook
Brisbane, Queensland
26753 posts
cpu?

cpu = computershare
look us up on the asx
Send Private Message
05:59pm 29/10/09 Permalink
Raven
Melbourne, Victoria
3898 posts
stringname[n-1]

Fixed.

Though given the simplicity, the question is probably not what it seems.

You assume, of course, that it's a null-terminated string and not a crazy length-specified variant.
Send Private MessageWWW
06:11pm 29/10/09 Permalink
parabol
Brisbane, Queensland
5485 posts
You assume, of course, that it's a null-terminated string

Actually my assumption was that the characters of the string are in contiguous memory, zero-indexed. The termination token can be arbitrary or not present at all. pew pew.
Send Private Message
06:33pm 29/10/09 Permalink
Raven
Melbourne, Victoria
3899 posts
Oh DIAF.
:)
Send Private MessageWWW
06:37pm 29/10/09 Permalink
Dazhel
Gold Coast, Queensland
429 posts
ack! clazy C style null terminated string handling!

System.String.Substring() gets the thumbs up.
Send Private Message
07:32pm 29/10/09 Permalink
Jim
Brisbane, Queensland
10621 posts
nah
Send Private Message
08:28pm 29/10/09 Permalink
euphoria
Gold Coast, Queensland
1489 posts
LOLCODE > *

HAI

CAN HAS STDIO?
VISIBLE "HAI WORLD!"
KTHXBYE

http://lolcode.com/home
Send Private Message
08:58pm 29/10/09 Permalink
Farseeker
Brisbane, Queensland
1602 posts
>> "whheee"[3..5]

=> "eee"


http://tryruby.sophrinix.com/
Send Private Message
10:21pm 29/10/09 Permalink
Dazhel
Gold Coast, Queensland
430 posts

>>> "whheee"[3:6]
'eee'


http://www.python.org/
Send Private Message
12:40am 30/10/09 Permalink
Mantra
Crusty old man
Brisbane, Queensland
2486 posts
System.String.Substring()
Daz speaks the truth.

Fuck, I've just gone back ... 10 years!
Send Private MessageWWW
01:04am 30/10/09 Permalink
Mantra
Crusty old man
Brisbane, Queensland
2487 posts
ps - Trim
Send Private MessageWWW
01:05am 30/10/09 Permalink
Rukh
Brisbane, Queensland
689 posts
Actually for it to be correct for the QGL forums:
(assuming a string called: stringname and saving the nth character in c)

int minimum = 0;


c = stringname[minimum + n - 1];


(Because surely 0 is the minimum index right Obes and Jim?)

;P

last edited by Rukh at 01:12:40 30/Oct/09
Send Private MessageWWW
01:11am 30/10/09 Permalink
Mantra
Crusty old man
Brisbane, Queensland
2489 posts
oh ffs...

*checks his calendar again*
Send Private MessageWWW
01:25am 30/10/09 Permalink
Obes
Brisbane, Queensland
8098 posts
I have no issue with a 0 indexed array... however you can't have 0 apples.
Send Private Message
07:15am 30/10/09 Permalink
mittens
Brisbane, Queensland
185 posts
you can have a zeroth apple though
Send Private Message
07:28am 30/10/09 Permalink
Jim
Brisbane, Queensland
10625 posts
nah ya can't
Send Private Message
08:35am 30/10/09 Permalink
Dazhel
Gold Coast, Queensland
431 posts
hold on, I thought zero was the minimum number of apples you could have?
Send Private Message
09:15am 30/10/09 Permalink
Jim
Brisbane, Queensland
10627 posts
that's because you're confused
Send Private Message
09:29am 30/10/09 Permalink
Hogfather
Cairns, Queensland
3931 posts
So I need to 1-index Apple arrays?
Send Private MessageWWW
09:32am 30/10/09 Permalink
Jim
Brisbane, Queensland
10629 posts
not if you have a healthy human brain
Send Private Message
09:36am 30/10/09 Permalink
Hogfather
Cairns, Queensland
3932 posts
I'm not your friend anymore Jim.
Send Private MessageWWW
09:37am 30/10/09 Permalink
Jim
Brisbane, Queensland
10630 posts
hey you're learning
that's heaps better than trying to count the absence of a friendship
Send Private Message
09:40am 30/10/09 Permalink
Obes
Brisbane, Queensland
8100 posts
If you don't have any apples how can you have 0 of them ? It's illogical!!!
Send Private Message
10:13am 30/10/09 Permalink
Showing 1 to 47 of 47 posts
Show: per page
1
Post a Reply
You must be logged in to post a reply.
 



Advertise with Us | Privacy Policy | Contact Us
© Copyright 2001-2009 AusGamers™ Pty Ltd. ACN 093 772 242.
A Mammoth Media web development.