簡體   English   中英

ANSI C分割字串

[英]ANSI C splitting string

嘿! 我陷入了一個ANSI C問題,我認為這應該是微不足道的(至少在任何現代語言中都是這樣:/)。

我的腳本的(臨時)目標是將一個字符串(由char組成的字符串)分割為6個字符(“ 123:45”),該字符串代表時間戳記mins:seconds(對於音頻文件而言,可以有120分鍾)僅分為分鍾和幾秒鍾。

我嘗試了幾種方法-一種通用的方法是尋找“:”,另一種是硬編碼的方法只是按索引將字符串分開,但似乎沒有用。

void _splitstr ( char *instr, int index, char *outstr ) {
char temp[3]; 
int i; 
int strl = strlen ( instr );
if ( index == 0 ) {
    for ( i = 0; i < 3; ++i ) {
        if ( temp[i] != '\0' ) {
            temp[i] = instr[i];
        }
    }
} else if ( index == 1 ) {
    for ( i = 6; i > 3; i-- ) {
            temp[i] = instr[i];
        }
    }
strcpy ( outstr, temp );
}

另一個“有趣”的事情是,char [3]的字符串長度為6或9,而從不實際為3。這有什么問題?

如何使用sscanf() 盡可能簡單。

    char time[] = "123:45";
    int minutes, seconds;

    sscanf(time, "%d:%d", &minutes, &seconds);

如果您可以確保時間字符串語法始終有效,則此方法效果最好。 否則,您必須為此添加檢查。 成功后,sscanf函數將返回成功讀取的項目數,因此也很容易檢測到錯誤。

工作示例: http : //ideone.com/vVoBI

怎么樣...

int seconds, minutes;
minutes = atoi(instr);
while(*instr != ':' && *++instr != '\0');
seconds = atoi(instr);

應該相當快。

您可以嘗試類似的方法:

void break_string(const char* input, char* hours, char* minutes)
{
    if(input == 0 || hours == 0 || minutes == 0)
        return;

    while(*input != ':')
        *hours++ = *input++;

    *hours = '\0';
    ++input;

    while(*minutes++ = *input++);

    return;
}

這是相同的功能,有點簡化了:

void break_string(const char* input, char* hours, char* minutes)
{
    if(input == 0 || hours == 0 || minutes == 0)
        return;

    while(*input != ':')
    {
        *hours = *input;
        ++hours;
        ++input;
    }
    *hours = '\0';

    ++input; //ignore the ':' part
    while(*input)
    {
        *minutes = *input;
        ++minutes;
        ++input;
    }
    *minutes = '\0';

    return;
}

int main()
{
    char* test = "123:45";
    char* minutes   = malloc( sizeof(char) * 12 );
    char* hours     = malloc( sizeof(char) * 12 );
    break_string( test , hours , minutes );
    printf("%s , %s \n" , hours , minutes ); 
    //...
    free( minutes );
    free( hours ) ;
}

您基本上有三種選擇

  • 更改輸入字符串(不能是字符串文字)
  • 將數據復制到輸出字符串(輸入可以是文字)
  • 將字符序列轉換為數字

更改輸入字符串意味着將"123:45""123\\0" "45" ,並嵌入一個空值。

復制數據意味着管理副本的存儲。

轉換字符序列意味着使用例如strtol

您沒有在temp []中的字符串上添加終止null,因此當您執行strlen(temp)時,您正在訪問任意內存。

使用已知的長度,您可以使用類似以下的內容:

char temp[4];
if (index==0)
{
  strncpy(temp, instr, 3);
  temp[3] = 0;
}
else if (index==1)
{
  strncpy(temp, instr+4, 2);
  temp[2] = 0;
}
strcpy(outstr, temp);

但是,請注意,我已經跳過了所有檢查instr和outstr中有效長度的檢查。

這個?

char *mins, *secs;
mins = str;
while(*(++str) != ':');
str[0] = '\0';
secs = s + 1;

這是一種方法,我忽略了上面的“ index”參數:

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

void _splitstr ( char *instr, char *outstr ) {
        char temp[10];
        char* end = strchr(instr, ':');
        int i = 0;

        if(end != 0) {
                while(instr != end)
                        temp[i++] = *instr++;
                temp[i] = '\0';
                strcpy(outstr, temp);
        } else {
                outstr = '\0';
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM