对数组类型为ch=ch+;(s[i]);

对数组类型为ch=ch+;(s[i]);,c,loops,for-loop,do-while,c-strings,C,Loops,For Loop,Do While,C Strings,我无法将字符指针数组s的字符存储到字符数组ch中: int main() { char *s; s = malloc(1024 * sizeof(char)); scanf("%[^\n]", s); s = realloc(s, strlen(s) + 1); char ch[50]; int i; s=s+' '; for(i=0;i<=strlen(s)+1;i++) { if(s[i]!

我无法将字符指针数组s的字符存储到字符数组ch中:

    int main() {
    char *s;
    s = malloc(1024 * sizeof(char));
    scanf("%[^\n]", s);
    s = realloc(s, strlen(s) + 1);
    char ch[50];
    int i;
    s=s+' ';
    for(i=0;i<=strlen(s)+1;i++)
    {
        if(s[i]!=' ')
        {
            ch= ch+(s[i]);
        }
        else
        {
            printf("%c \n",ch);
            ch=' ';
        }
    }
    return 0;
}

您的代码中有许多错误(一些错误在注释中指出)。这是一个可行的解决方案,它符合我的想法您需要的:

#include <stdio.h>
#include <stdlib.h>

int main() {
    char* s;
    s = malloc(1024 * sizeof(char));
    scanf("%[^\n]", s);
    s = realloc(s, strlen(s) + 2); // Add 2 because you later append a space!
    //Write your logic to print the tokens of the sentence here.
    //i
    char ch[50] = { '\0' }; // Make sure you initialise the buffer!
    int i;
//  s = s + ' '; // This doesn't do what you think - it's adding the value of ' ' to the string's address!
    strcat(s, " "); // This appends a blank to the string!
    for (i = 0; i < strlen(s); i++) // For 7 chars, will do [0] thru [6]!
    {
        if (s[i] != ' ')
        {
        //  ch = ch + (s[i]); // You can't just 'add; an element to an array!
            ch[i] = s[i];
        }
        else
        {
        //  printf("%c \n", ch);
            printf("%s \n", ch); // Use this to print the entire ch string so far!
            ch[i] = ' ';
        }
    }
    return 0;
}
#包括
#包括
int main(){
char*s;
s=malloc(1024*sizeof(char));
scanf(“%[^\n]”,s);
s=realloc(s,strlen(s)+2);//添加2,因为您稍后会追加一个空格!
//在这里写下你的逻辑来打印句子的标记。
//我
char ch[50]={'\0'};//确保初始化缓冲区!
int i;
//s=s+'';//这与您的想法不符-它将“”的值添加到字符串的地址中!
strcat(“”;//这会在字符串后面追加一个空格!
对于(i=0;i

我已经把注释放在了我修改代码的地方;请随时要求进一步解释

首先,这是一个循环

for(i=0;i<=strlen(s)+1;i++)

没有道理。数组没有运算符
+
,它们是不可修改的左值

for循环不适合这样的任务,因为在循环中不会输出最后一个字

你的意思如下

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) 
{
    char *s;
    s = malloc(1024 * sizeof( char ) );

    scanf("%1023[^\n]", s);
    s = realloc(s, strlen(s) + 1);


    char ch[50] = { '\0' };

    size_t i = 0, j = 0;

    do
    {
        if ( s[i] != ' ' && s[i] != '\t' && s[i] != '\0' )
        {
            ch[j++] = s[i];
        }
        else if ( ch[0] != '\0' )
        {
            ch[j] = '\0';
            puts( ch );
            j = 0;
            ch[j] = '\0';
        }
    } while ( s[i++] != '\0' );

    return 0;
}
然后程序输出将是

Hello
muskan
litw

ch
是一种数组类型。它不能用作赋值运算符的左操作数。。。。整个数组也没有正确地对应于
%c
printf指令。你的意思可能是
ch[i]
(几乎在循环体中的任何地方)?。。。而
iBTW在C语言中没有真正的字符串类型,你可以在其中执行类似
mystring=yourstring+“Hello”
等操作。阅读C语言教科书中有关字符串的章节。什么是
s=s+'
?那是行不通的。(这将使
s
超出原来的32个位置。)比我的答案更好!您对期望输出的假设也可能是正确的。你得到我的紫外线!非常感谢。我仍然怀疑为什么我们需要再增加两个空间?一个用于空格,我理解,但另一个用于什么?函数返回字符串的长度(字符),但不包括终止的
nul
字符。因此,对于字符串,“abcde”
strlen
将返回一个值5…但我们需要一个包含6个字符的数组来存储它:“a”、“b”、“c”、“d”、“e”和“nul”。这有意义吗?
ch=' ';
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) 
{
    char *s;
    s = malloc(1024 * sizeof( char ) );

    scanf("%1023[^\n]", s);
    s = realloc(s, strlen(s) + 1);


    char ch[50] = { '\0' };

    size_t i = 0, j = 0;

    do
    {
        if ( s[i] != ' ' && s[i] != '\t' && s[i] != '\0' )
        {
            ch[j++] = s[i];
        }
        else if ( ch[0] != '\0' )
        {
            ch[j] = '\0';
            puts( ch );
            j = 0;
            ch[j] = '\0';
        }
    } while ( s[i++] != '\0' );

    return 0;
}
Hello muskan litw
Hello
muskan
litw