繁体   English   中英

按空格将数组拆分为单词

[英]Split array by space into words

我在这里得到了这个代码,它有效。

#include <stdio.h>
#include <string.h>
int main()
{
    char str1[100] = "Hello how are you";
    char newString[10][10];
    int i,j,ctr;
       printf("\n\n Split string by space into words :\n");
       printf("---------------------------------------\n");


    j=0; ctr=0;
    for(i=0;i<=(strlen(str1));i++)
    {
        // if space or NULL found, assign NULL into newString[ctr]
        if(str1[i]==' '||str1[i]=='\0')
        {
            newString[ctr][j]='\0';
            ctr++;  //for next word
            j=0;    //for next word, init index to 0
        }
        else
        {
            newString[ctr][j]=str1[i];
            j++;
        }
    }
    printf("\n Strings or words after split by space are :\n");
    for(i=0;i < ctr;i++)
        printf(" %s\n",newString[i]);
    return 0;
}

但不是char str1[100]我想使用句子数组char str1[2][100]含义

char str1[2][100] = {"Hello how are you","I'm good, thanks"} 

而这两个句子(或更多)我想用单独的词分开

char str1[100] = {"Hello","how","are","you"};

实际上,这是来自学校的一个项目,在该项目中,我必须存储以“。”结尾的每个句子。

如果有另一种方法可以将每个句子直接存储为单词而不是句子,那将有很大帮助。

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

#define LSIZ 128
#define RSIZ 10

void yodificacio(char* arr[], int index[], int n)
{
    char* temp[n];

    // arr[i] should be present at index[i] index
    for (int i=0; i<n; i++){
        temp[index[i]] = arr[i];
    }
    // Copy temp[] to arr[]
    for (int i=0; i<n; i++)
    {
        arr[i] = temp[i];
        index[i] = i;
    }
}

int main()
{
    int i = 0;

    char *arr[] = {"Hey","there","how","are","you","all","today","idk"}; **Here I want the input to be char str1[2][100] = {"Hello how are you", "Im good thanks}, instead of char arr[] ...**             
    int index[] = {0,2,1,4,5,3,6,7};                  
    int n = sizeof(arr)/sizeof(arr[0]);

    yodificacio(arr, index, n);

    printf("Reordered array is: \n");
    for (int i=0; i<n; i++)
        printf ("%s ", arr[i]);
    return 0;



    return 0;
}

添加一个外部循环来处理数组中的每个句子。

int main()
{
    char str1[2][100] = {"Hello how are you","I'm good, thanks"} ;
    char newString[10][10];
    int i,j,ctr;
       printf("\n\n Split string by space into words :\n");
       printf("---------------------------------------\n");


    j=0; ctr=0;
    for (int k = 0; k < sizeof(str1) / sizeof(str1[0]); k++) {
        for(i=0;i<=(strlen(str1));i++)
        {
            // if space or NULL found, assign NULL into newString[ctr]
            if(str1[k][i]==' '|| str1[k][i]=='\0')
            {
                newString[ctr][j]='\0';
                ctr++;  //for next word
                j=0;    //for next word, init index to 0
            }
            else
            {
                newString[ctr][j]=str1[k][i];
                j++;
            }
        }
    }
    printf("\n Strings or words after split by space are :\n");
    for(i=0;i < ctr;i++)
        printf(" %s\n",newString[i]);
    return 0;
}

暂无
暂无

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

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