[英]How to cut part of a string in c?
我想弄清楚如何在 C 中剪切字符串的一部分。例如,你有這個字符串“狗死了,因為汽車在過馬路時撞到了他”一個函數如何去使句子“a過馬路時汽車撞了他”或“汽車撞了他”
你如何使用 C 的庫(或/和)自定義函數來解決這個問題?
好的,我沒有主要代碼,但這將是這個實驗的結構
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include "display_usage.c"/*If the user enters wrong arguments it will tell them how it should be */
void cut( const char *file, int option, int first, int last );
int main(int argc, char *argv[] ) {
FILE *fp;
char ch;
fp = fopen("test.txt", "r"); // Open file in Read mode
while (ch!=EOF) {
ch = fgetc(fp); // Read a Character
printf("%c", ch);
}
fclose(fp); // Close File after Reading
return 0;
}
void cut( const char *file, int reverse, int first, int last ) {
return;
}
strncpy
最多只能復制n
字符。 或者,您可以在字符串中移動指針,如果您有可寫內存,還可以將\\0
粘貼到數組中以盡早終止它。
以下函數從字符緩沖區中剪切給定范圍。 范圍由起始索引和長度標識。 可以指定負長度以指示從起始索引到字符串結尾的范圍。
/*
* Remove given section from string. Negative len means remove
* everything up to the end.
*/
int str_cut(char *str, int begin, int len)
{
int l = strlen(str);
if (len < 0) len = l - begin;
if (begin + len > l) len = l - begin;
memmove(str + begin, str + begin + len, l - len + 1);
return len;
}
通過使用memmove
將范圍之后的所有內容(包括終止的'\\0'
到起始索引,從而覆蓋范圍,從而切出字符范圍。 范圍內的文本丟失。
請注意,您需要傳遞一個其內容可以更改的字符緩沖區。 不要傳遞存儲在只讀內存中的字符串文字。
對於這樣的問題,最好自己編寫函數,這需要時間,但會有所回報。 函數str_slice 的代碼如下所示,非常類似於 JavaScript 的函數string.slice ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice ) 和到 Python 的在字符串或數組上制作切片的功能( https://docs.python.org/3.5/library/functions.html#slice )。
它也僅基於 C 標准庫,因此必須是跨平台的並且可以與任何編譯器一起使用。 如有疑問,請查看測試。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/**
* Extracts a selection of string and return a new string or NULL.
* It supports both negative and positive indexes.
*/
char *
str_slice(char str[], int slice_from, int slice_to)
{
// if a string is empty, returns nothing
if (str[0] == '\0')
return NULL;
char *buffer;
size_t str_len, buffer_len;
// for negative indexes "slice_from" must be less "slice_to"
if (slice_to < 0 && slice_from < slice_to) {
str_len = strlen(str);
// if "slice_to" goes beyond permissible limits
if (abs(slice_to) > str_len - 1)
return NULL;
// if "slice_from" goes beyond permissible limits
if (abs(slice_from) > str_len)
slice_from = (-1) * str_len;
buffer_len = slice_to - slice_from;
str += (str_len + slice_from);
// for positive indexes "slice_from" must be more "slice_to"
} else if (slice_from >= 0 && slice_to > slice_from) {
str_len = strlen(str);
// if "slice_from" goes beyond permissible limits
if (slice_from > str_len - 1)
return NULL;
buffer_len = slice_to - slice_from;
str += slice_from;
// otherwise, returns NULL
} else
return NULL;
buffer = calloc(buffer_len, sizeof(char));
strncpy(buffer, str, buffer_len);
return buffer;
}
測試
#include <assert.h>
void
test_str_slice()
{
char str[] = "abcdefghijkl";
assert(NULL == str_slice(str, -3, -10));
assert(NULL == str_slice(str, -1, -2));
assert(NULL == str_slice(str, -1, 0));
assert(NULL == str_slice(str, 1, 0));
assert(NULL == str_slice(str, 5, 4));
assert(NULL == str_slice(str, 0, 0));
assert(NULL == str_slice(str, 10, 10));
assert(NULL == str_slice(str, -2, -2));
assert(NULL == str_slice(str, -20, -12));
assert(NULL == str_slice(str, -20, -13));
assert(NULL == str_slice(str, 12, 13));
assert(NULL == str_slice(str, 12, 20));
assert(NULL == str_slice("", 1, 2));
assert(NULL == str_slice("", -2, -1));
assert(strcmp(str_slice(str, -3, -1), "jk") == 0);
assert(strcmp(str_slice(str, -8, -3), "efghi") == 0);
assert(strcmp(str_slice(str, -10, -9), "c") == 0);
assert(strcmp(str_slice(str, -2, -1), "k") == 0);
assert(strcmp(str_slice(str, -15, -1), "abcdefghijk") == 0);
assert(strcmp(str_slice(str, -12, -2), "abcdefghij") == 0);
assert(strcmp(str_slice(str, -15, -8), "abcd") == 0);
assert(strcmp(str_slice(str, -15, -11), "a") == 0);
assert(strcmp(str_slice(str, 1, 3), "bc") == 0);
assert(strcmp(str_slice(str, 11, 100), "l") == 0);
assert(strcmp(str_slice(str, 2, 4), "cd") == 0);
assert(strcmp(str_slice(str, 3, 6), "def") == 0);
assert(strcmp(str_slice(str, 0, 1), "a") == 0);
assert(strcmp(str_slice(str, 4, 6), "ef") == 0);
assert(strcmp(str_slice(str, 1, 2), "b") == 0);
assert(strcmp(str_slice(str, 0, 3), "abc") == 0);
assert(strcmp(str_slice(str, 0, 11), "abcdefghijk") == 0);
assert(strcmp(str_slice(str, 2, 10), "cdefghij") == 0);
assert(strcmp(str_slice(str, 0, 50), "abcdefghijkl") == 0);
}
正如您在測試中看到的,該函數返回一個字符串或 NULL。 它還支持負指數和正指數。 這個想法取自前面提到的 JavaScript 和 Python 的早期特性。 所以,不要大量污染這個答案,我建議你閱讀 JavaScript 和 Python 的文檔。
如果您知道字符串的內容, strstr
對您來說將是完美的。
示例:
char *str = "A dog died because a car hit him while he was crossing the road.";
char *pCh = strstr(str, "dog");
pCh
將具有"dog"
'd'
的地址。
http://www.cplusplus.com/reference/cstring/
您可以使用 strstr(獲取子字符串)、strtok(使用某些標記拆分)等函數,
您可以通過簡單的代碼使用類似於 Python 的[n:m]
剪切運算符的東西,但涉及動態分配並保留作為輸入的原始字符串。
char* cutoff(const char* str, int from , int to)
{
if (from >= to)
return NULL;
char* cut = calloc(sizeof(char), (to - from) + 1);
char* begin = cut;
if (!cut)
return NULL;
const char* fromit = str+from;
const char* toit = str+to;
(void)toit;
memcpy(cut, fromit, to);
return begin;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.