c - 将字符串(单词)拆分为 C 中的字母

标签 c

我需要一个字符串来分割成字母并将其保存在一个数组中。我不知道如何做到这一点。在 C++ 中是可能的,但在 C 中似乎没有办法做到。

或者,如果有一种方法可以获取输入字符串(一个单词)并将其作为单独的字母保存在数组中将是理想的。我已经使用下面提到的代码来获取输入

    #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]);
    }

更新:这个问题已经解决了,我没有在这里声明一个指向字符的指针,这是缺少的部分,然后我们可以逐个字母地读取单词,谢谢大家

最佳答案

看看这段代码,看看你的错误

        #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 '\0' - a speial character indicating the end of string */
                while ( *p != '\0' ) {
                       // <- 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
        }

关于c - 将字符串(单词)拆分为 C 中的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32858256/

相关文章:

c - 为什么我的 bison/flex 没有按预期工作?

c - 线程有不同的堆吗?

c - 引用结构体值,但不断出现很多错误

c - 树莓派,与串口设备通信

java - C/Java 之间的类似代理系统的实现提示

c - 消耗指针是什么意思?

c++ - typedef 指针是个好主意吗?

python - 如何在python中递归计算pi?

c - 仅保存 C 数组的最后一个元素

c# - 有人使用 CLI 后端为 gcc 编译器编译 lib x264 吗?