Archived

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

Reading each char of a string

This topic is 5701 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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 on other sites
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 on other sites
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 on other sites
Shenron,
1) Ah, you go this now, as well .
2) You fixed it .

[edited by - Null and Void on June 8, 2002 3:02:33 AM]

Share on other sites
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 on other sites
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 on other sites
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 on other sites
#include <iostream>using namespace std;int main(){	char string[7] = "string";	char *pchar = &string[0];   <--- HINT HINT	cout << pchar << endl;	return 0;}

Share on other sites
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 on other sites
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! ;-)
 just put the [ i ] between [ c o d e ] and [ / c o d e ]!
 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 on other sites
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:

  // Declarationsint i;// Codefor (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)