Advertisement

How to make a string

Started by May 11, 2001 01:05 PM
7 comments, last by Ray745 23 years, 9 months ago
For any of you familiar with apstrings, is there anyway to convert an apstring to an array of characters and also is there any quick way to name a string out of an array of characters like char name[5]; name[0]=''H''; name[1]=''e''; and so on....is there any way to do that all in one line making the entire array equal Hello??? If I only had a nickle for every problem I''''ve had, I wouldn''''t have anymore problems, but then I guess I wouldn''''t have anymore nickles either...
If I only had a nickle for every problem I''ve had, I wouldn''t have anymore problems, but then I guess I wouldn''t have anymore nickles either...
char name[6]="Hello"; 

My c is a bit rusty, but that should work.

/Mikael Jacobson


AFA-GBG2001 [www.motkraft.net/gbg2001]
Advertisement
Use the Getche() function

something like

char name[5];

for(i=0;i <= 4; i++)
{
name = getche();
}

I know this is a ver poor and rough example, but it should accomplish waht you need if you modify it a little.


"There is humor in everything depending on which prespective you look from."

Edited by - chronoslade on May 11, 2001 2:50:39 PM
"There is humor in everything depending on which prespective you look from."
I am still a relative newbie but to assign a string of characters to an array shouldn''t

char array[] = "hello"
work? that makes an array of 6 elements including the /n.
or
char *array = "hello"

then again i could be misunderstanding.

Blaze
Zen, its all about Zen.
Those are great suggestions if you want to see what''s in memory from the point where your string starts to the point where the first random null character appears.
Blaze, yeah both of those should work. If you use:
char array[] = "hello";

and don''t put a number between the brackets it will automatically only use the amount of array elements as it needs



A CRPG in development...

Need help? Well, go FAQ yourself.
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Advertisement
As everyone said: char name[6] = "Hello"; will do the trick (The 6th character is actually the null-terminated character that is a must...).
Or:
char name[6];
name[0] = ''H'';
name[1] = ''e'';
name[2] = ''l'';
name[3] = ''l'';
name[4] = ''o'';
name[5] = ''\0'';

You mustn''t forget adding the last character that is the null-terminated character.



- Goblineye Entertainment
The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
If I understand you correctly, you are using an class called "apstring." If this is the case, you would just declare an "apstring" object and set it equal to a word. Here is an example:
apstring name = "Hello";
"H" would be in the name[0] position.

If I misunderstood you, and you are not using this class, you could just use the "string" class, which is located in the standard library of your developement software.




Edem Attiogbe
Edem Attiogbe
I forgot, you could also simply use a character pointer like Blazemore said. If you just use a character, and not a character pointer, don''t forget the null terminator.

Edem Attiogbe
Edem Attiogbe

This topic is closed to new replies.

Advertisement