-2

文字列を分割し、2 つの個別の変数に格納する必要があります。文字列にタブ スペースが含まれています。そのため、タブスペースから分離する必要があります

例: 文字列は次のようになります

Sony <TAB>         A Hindi channel.

Sonyある変数に保存し、別の変数に保存する必要がありchar a[6];ますA Hindi Channelchar b[20];

どうすればこれを行うことができますか?

4

3 に答える 3

0

私のCは古いですが、そのようなものはうまくいくはずです:

#include <stdio.h>


int getTabPosition (char str [])
{
    int i = 0;
    //While we didn t get out of the string
    while (i < strlen(str))
    {
        //Check if we get TAB
        if (str[i] == '\t')
            //return it s position
            return i;
        i = i + 1;
    }
    //If we get out of the string, return the error
    return -1;
}

int main () {
    int n = 0;
    //Source
    char str [50] = "";
    //First string of the output
    char out1 [50] = "";
    //Second string of the output
    char out2 [50] = "";

    scanf(str, "%s");
    n = getTabPosition(str);
    if (n == -1)
        return -1;
    //Copy the first part of the string
    strncpy(str, out1, n);
    //Copy from the end of out1 in str to the end of str
    //str[n + 1] to skip the tab
    memcpy(str[n+1], out2, strlen(str) - n);
    fprintf(stdout, "Original: %s\nout1=%s\nout2=%s", str, out1, out2);
    return 0;
}

テストされていませんが、原則はあります

于 2013-10-10T08:16:27.690 に答える