Archived

This topic is now archived and is closed to further replies.

stealth

Reading each char of a string

Recommended Posts

stealth    100
Well say I had a char array something like: char string[5] = "char"; how do I read each character out of the string?, so I seperate each character like: first I get the c then the h then the a then the r , thanks to anyone who can help me with this ( oh yeah I''m not using windows ) Cheers Stealth

Share this post


Link to post
Share on other sites
Xanth    122
You just reference them like you would any Array.

i.e.

char string[5] = "char";

string[0] returns ''c''
string[1] returns ''h''

etc...

"I thought Genius lived in bottles..." - Patrick Star

Share this post


Link to post
Share on other sites
Shenron    122
I don't know if you want to do it in c or c++ but you can do this in c and basicly the same thing with cout in c++:

  
char string[5] = "char";

putchar(string[0]);
putchar(string[1]);
putchar(string[2]);
putchar(string[3]);


or with a for loop like this:

    
char string[5] = "char";
int i;

for(i = 0; i < 4; i++)
putchar(string);

it's supposed to be string[i] in there but it disappears when i post it because [i] is used to italics on this board i guess.

[edited by - Shenron on June 8, 2002 2:53:30 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
You can also use a pointer to access the character array. Like this:

char string[5] = "char";
char *charPtr = string; //Or char *charPtr = &string[0];
char theChar;

while( ((theChar = *charPtr++) != ''\0'' ) putchar( theChar );

Kory

Share this post


Link to post
Share on other sites
stealth    100
hey thanks for the replies but I THINK I figured it out I havent tried yet but I think I got the idea

thansk anyway

cheers

Stealth

Share this post


Link to post
Share on other sites
stealth    100
Ok I KINDA got it working except its now giving me the chars backwards, I mean if I go something like:

char string[7] = "string";
char *pchar = &string[3];

int main()
{
cout << pchar;
}


then it will print "ing" the last 3 letters of the string, I was wondering if there was way to read them from the start????, this is something I need to know since the strings I will be reading will be 256 chars long most probably and I dont wanna have to read thru all of them backwards to get to the start, OR maybe I should read them until I get a blank space, oh nah sorry I''m confusing u all lol I think I got it


Share this post


Link to post
Share on other sites
Shenron    122

#include <iostream>
using namespace std;


int main()
{
char string[7] = "string";
char *pchar = &string[0]; <--- HINT HINT

cout << pchar << endl;


return 0;
}

Share this post


Link to post
Share on other sites
Jason Zelos    211
Your pointer tells cout where the begining of the string is, the end is either zero terminated or NULL terminated (I forget which, NULL I think). So your setting the pointer to ''i'' and reading to the end.

To grab substrings and find chars in a string look at the string.h lib for some useful functions. (or use the STD string class).

,Jay

Share this post


Link to post
Share on other sites
Pipo DeClown    804
cpp-style:

for(int i=0;i<4;i++)
cout<<<' ';


c-style::

for(int i=0;i<4;i++)
printf("%c ",string[i]);

i'm not sure about c-style, i compile in cpp! ;-)
[edit] just put the [ i ] between [ c o d e ] and [ / c o d e ]!
[edit] backwards huh?:

for(int i=6;i!=0;i--) //suppose u use string as sample now...
cout<<<' ';



[edited by - Pipo declown on June 8, 2002 10:47:45 AM]

Share this post


Link to post
Share on other sites
jjmontes    158
quote:

[source
for(int i=0;i<4;i++)
printf("%c ",string);
[/source
i''m not sure about c-style, i compile in cpp! ;-)



In C, you cannot declare variables in a program section, you must do it at the beggining, so that for(int i=...) is incorrect:


  
// Declarations

int i;
// Code

for (i=0;i<4;i++) printf ("%c",string[i]);


By the way, in C++ is not neccesary to use string.h to get an unique char from a pointer. Just use an asterisk to interpretate that "pointer to char" as a "char":

[souce]
char String[5]="Soul";
char *pString = String;
while (*pString != ''\0'')
{
cout << *pString;
}
[/source]

Real programers are not afraid from maths! (I am)
(from an Asfixia Member I think)
JJpRiVaTe (Private Zone)

Share this post


Link to post
Share on other sites