Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

Split a string(a word) to letters in C

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

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

1 Reply

0 votes
by (71.8m points)

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
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...