如何将一段不确定长度的句子拆分成单个字符输入数组中

来源:百度知道 编辑:UC知道 时间:2024/06/14 18:54:11
可以使用指针和动态内存分配,本人大一,请使用比较简单的语句实现
不能使用puts,gets ,strcat,strcpy等语句 ,并且遇空格不会停止输入,例如我定义个不确定大小的数组s[],输入the 日result of 3 and 2 is not 8,它会自动变成s[]={'t','h',......'8'}这样的,写详细点

开一个大数组就可以了。
#include <stdio.h>
#include <stdlib.h>
void main()
{
char s[1024]; // 大数组
int i,n;
n=0;
scanf("%c",&s[n]); // 读一个字符的方法

while (s[n] !='\n') //如果没有读到换行符,就一个一个读下去
{
n=n+1;
scanf("%c",&s[n]);
}
for (i=0;i<n;i++) printf("'%c'",s[i]); // 打出结果
}
-----------------------
如果存放时,你要数组里不存放空白,则把语句n=n+1; 改成:
if ( s[n] != ' ') n=n+1;

用一个循环一个字符一个字符的复制过来,直到遇到'\0'为止.
C语言里面不允许定义不确定大小的数组.你定义一个足够大的数组就行了
#include <stdio.h>
main()
{
int i=0;
int c;
char a[100],b[100];
printf("input your string:\n");
while ((c=getchar())!='\n')
{
a[i++]=c;
}
a[i]='\0';

for(i=0;a[i]!='\0';i++)
{
b[i]=a[i];
}
b[i]='\0&#