我需要一个字符串来分割成字母并将其保存在一个数组中。我不知道如何做到这一点。在 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/