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.
|
tequila
Brisbane, Queensland
3727 posts
redhat
Sydney, New South Wales
525 posts
nF
Forum Hero
Wynnum, Queensland
16124 posts
parabol
Brisbane, Queensland
5479 posts
stringname[n-1]
Fixed.
Though given the simplicity, the question is probably not what it seems.
|
Jim
Brisbane, Queensland
10610 posts
nF
Forum Hero
Wynnum, Queensland
16125 posts
As if you wouldn't be counting from zero.
|
Scooter
Brisbane, Queensland
2096 posts
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
|
redhat
Sydney, New South Wales
526 posts
Am counting from zero, :P
Worked it out thanks guys.
|
Obes
Brisbane, Queensland
8091 posts
Unless its apples you always count from 0
|
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
|
Jim
Brisbane, Queensland
10613 posts
Spook
Brisbane, Queensland
26748 posts
redhat is very bad at programming
u need ansi c by denis ritchie and some skillz
|
Opec
Brisbane, Queensland
5946 posts
Hogfather
Cairns, Queensland
3916 posts
 Hahah hi Perlboy
|
redhat
Sydney, New South Wales
528 posts
Thanks guys.
Stupid engine written in C, WTB perl.
|
Hogfather
Cairns, Queensland
3918 posts
Stupid engine written in C, WTB perl Hahah what the fuck ... first time this phrase has ever been uttered?!
|
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
|
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
|
mongie
Brisbane, Queensland
6785 posts
redhat
Sydney, New South Wales
529 posts
perl is just easier because i've done it before :P
edit: ended up using sprintf!
|
Jim
Brisbane, Queensland
10617 posts
use snprintf
pretend sprintf doesn't exist
|
jay
Brisbane, Queensland
54 posts
you almost had us fooled thinking you were competent at your job ry.
|
Spook
Brisbane, Queensland
26753 posts
cpu?
cpu = computershare
look us up on the asx
|
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.
|
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.
|
Raven
Melbourne, Victoria
3899 posts
Dazhel
Gold Coast, Queensland
429 posts
ack! clazy C style null terminated string handling!
System.String.Substring() gets the thumbs up.
|
Jim
Brisbane, Queensland
10621 posts
euphoria
Gold Coast, Queensland
1489 posts
Farseeker
Brisbane, Queensland
1602 posts
Dazhel
Gold Coast, Queensland
430 posts
Mantra
Crusty old man
Brisbane, Queensland
2486 posts
System.String.Substring() Daz speaks the truth.
Fuck, I've just gone back ... 10 years!
|
Mantra
Crusty old man
Brisbane, Queensland
2487 posts
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
|
Mantra
Crusty old man
Brisbane, Queensland
2489 posts
oh ffs...
*checks his calendar again*
|
Obes
Brisbane, Queensland
8098 posts
I have no issue with a 0 indexed array... however you can't have 0 apples.
|
mittens
Brisbane, Queensland
185 posts
you can have a zeroth apple though
|
Jim
Brisbane, Queensland
10625 posts
Dazhel
Gold Coast, Queensland
431 posts
hold on, I thought zero was the minimum number of apples you could have?
|
Jim
Brisbane, Queensland
10627 posts
that's because you're confused
|
Hogfather
Cairns, Queensland
3931 posts
So I need to 1-index Apple arrays?
|
Jim
Brisbane, Queensland
10629 posts
not if you have a healthy human brain
|
Hogfather
Cairns, Queensland
3932 posts
I'm not your friend anymore Jim.
|
Jim
Brisbane, Queensland
10630 posts
hey you're learning
that's heaps better than trying to count the absence of a friendship
|
Obes
Brisbane, Queensland
8100 posts
If you don't have any apples how can you have 0 of them ? It's illogical!!!
|
|