Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need a string to split into its letters and save it in an array .I have no clue how to do this .Its possible in C++ but in C it seems like there is no way to do.

Or if there is a way to take an input string(a word )and save it as separate letters in an array will be ideal .I have used below mentioned code to get the input

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include<string.h>
    int(){
char inputnumber;
        printf(  "Enter the number" ); 
// the word is like --> hellooo
         scanf("%s", &inputnumber);
int i=0;
printf(inputnumber[i]);
    }

Update: this is solved ,I did not declare a pointer to the char here ,that's the part which is missing then we can read the word letter by letter ,thanks all

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
140 views
Welcome To Ask or Share your Answers For Others

1 Answer

look at this code and see your mistakes

        #include <stdio.h>
    //  #include <stdlib.h> // you dont really need this
    //  #include <math.h> // or this
    //  #include<string.h> // or this
        //int(){
        int main (){ // <-- int main here
                printf(  "Enter the number" );
                // declare a character array to Store input String
                char inputnumber[126];
                scanf("%s", &inputnumber);

                /** take a pointer to point to first character **/
                char *p = inputnumber;

                /** iterate through it untill you get '' - a speial character indicating the end of string */
                while ( *p != '' ) {
                       // <- print characters not strings hence c
                      //based on your requirement you can store this in another array
                        printf ("%c ", *p  ); 

                        p++ ; // move p to point to next position
                }

                return 0; // return happyily
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...