簡體   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