Advertisement

Reading each char of a string

Started by June 08, 2002 12:11 AM
9 comments, last by stealth 22 years, 6 months ago
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
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
"I thought Genius lived in bottles..." - Patrick Star
Advertisement
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 in there but it disappears when i post it because is used to italics on this board i guess.<img src="wink.gif" width=15 height=15 align=middle><br><br><SPAN CLASS=editedby>[edited by - Shenron on June 8, 2002 2:53:30 AM]</SPAN> </i>
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]
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
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
Advertisement
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


#include <iostream>using namespace std;int main(){	char string[7] = "string";	char *pchar = &string[0];   <--- HINT HINT	cout << pchar << endl;	return 0;} 
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
cpp-style:
for(int i=0;i<4;i++)cout<<<' ';      


c-style::
for(int i=0;i<4;i++)printf("%c ",string);<br>      </pre>      </font><br>i'm not sure about c-style, i compile in cpp! ;-)       </i>        <br>[edit] just put the  between [ c o d e ] and [ / c o d e ]!<br>[edit] backwards huh?:<font color=red><br><pre><br>for(int i=6;i!=0;i–)  //suppose u use string as sample now…<br>cout<<string<i><<' ';<br>    </pre>    </font><br><br><br><SPAN CLASS=editedby>[edited by - Pipo declown on June 8, 2002 10:47:45 AM]</SPAN>  </i>   

This topic is closed to new replies.

Advertisement