简体   繁体   English

为什么我不能分隔字符串中的字母?

[英]Why can't I separate letters in a string?

I want to take all the letters in a string and put it in a array separately.我想把一个字符串中的所有字母分别放在一个数组中。 But I am receiving some error and I could not figure out.但我收到一些错误,我无法弄清楚。

10 20 E:\FALL SEM 20-21\CS\C codes\Untitled3.c [Warning] passing argument 2 of 'strcpy' makes pointer from integer without a cast 10 20 E:\FALL SEM 20-21\CS\C 代码\Untitled3.c [警告] 传递 'strcpy' 的参数 2 使来自 integer 的指针没有演员表

My code is我的代码是

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


char array[10][100],string[100];
int top=0;

void push(char elem)
{
    strcpy(array[top],elem);
    top++;
}

int main()
{
    printf("Enter the string: \n");
    fgets(string,100,stdin);
    int length;
    length=strlen(string);
    int i=0;
    while((string[i])!='\0')
    {
        push(string[i]);
        i++;
    }
    printf("%d",length);
}

strcpy() is for copying strings (sequences of characters terminated by a null-character). strcpy()用于复制字符串(以空字符结尾的字符序列)。 To use that, you should make strings from the characters and pass them.要使用它,您应该从字符中创建字符串并传递它们。 Also you have to fix the type of the argument of push() .您还必须修复push()参数的类型。

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


char array[10][100],string[100];
int top=0;

void push(const char* elem) /* use const char* to receive strings that won't be modified */
{
    strcpy(array[top],elem);
    top++;
}

int main(void)
{
    printf("Enter the string: \n");
    fgets(string,100,stdin);
    int length;
    length=strlen(string);
    int i=0;
    while((string[i])!='\0')
    {
        char str[2] = {string[i], '\0'}; /* create a string */
        push(str); /* and push that */
        i++;
    }
    printf("%d",length);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM