I need to convert a string to a char array in C; how can I do this?

Or at least, how can I extract single chars from a string incrementally?

2

3 Answers

In C, a string is actually stored as an array of characters, so the 'string pointer' is pointing to the first character. For instance,

char myString[] = "This is some text"; 

You can access any character as a simple char by using myString as an array, thus:

char myChar = myString[6]; printf("%c\n", myChar); // Prints s 

Hope this helps! David

0

In C, there's no (real, distinct type of) strings. Every C "string" is an array of chars, zero terminated.

Therefore, to extract a character c at index i from string your_string, just use

char c = your_string[i]; 

Index is base 0 (first character is your_string[0], second is your_string[1]...).

3

In this simple way

char str [10] = "IAmCute"; printf ("%c",str[4]); 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy