Tag: substring

释放c – loop中的子串

我正在尝试为结构’ structs ‘的每个成员获取一个子字符串,然后将该子字符串分配给temp_struct的新成员。 我遇到的问题是如何在每次迭代时释放子字符串,由于某种原因代码运行,但是valgrind抛出一个Invalid read of size 1的Invalid read of size 1 ,我假设我正在读取内存块。 我怎么能释放子串呢? 谢谢 #include #include #include struct st_ex { char product[16]; float price; }; struct st_temp { char *prod; }; char *temp = NULL; // from stackoverflow char* substr( const char* source, size_t start, size_t end ) { char* dest = malloc( end […]

检查字符串中是否存在所有char值

我正在做这个任务,我被困住了。 目标是读取文件并查找文件中String中是否存在这些char值。 我必须将文件中的String与作为参数放入的另一个String进行比较。 但是,只要每个char值都在文件的String中,那么它就“匹配”。 示例(输入和输出): ./a.out file1完成 完成是在笨蛋 完成不是在小狗 示例(file1): 梼 狗 正如您所看到的那样,比较字符串无关紧要,文件每行也跟着一个单词。 我把一个程序放在一起,查找char值是否存在于另一个String中,但这只是问题的一部分。 知道如何去做吗? #define _GNU_SOURCE #include #include #include int main(int argc, char **argv){ FILE *f = fopen(argv[1], “r”); char *line = NULL; size_t len = 0; ssize_t read; char *word = argv[2]; if(argc != 3){ printf(“./a.out \n”); exit(EXIT_SUCCESS); } if(f == NULL){ printf(“file […]

strstr()函数

我正在尝试编写一个程序,将用户输入的子字符串与字符串数组进行比较。 #include #include char animals[][20] = { “dogs are cool”, “frogs are freaky”, “monkeys are crazy” }; int main() { char input[10]; puts(“Enter animal name: “); fgets(input, sizeof(input), stdin); int i; for(i = 0; i < 3; i++) { if(strstr(animals[i], input)) printf("%s", animals[i]); } return 0; } 当我进入青蛙时,例如它应该打印出“青蛙怪异”的信息,但它没有打印出来。 所以我试着写一行来每次打印出strstr()函数的值,它们都返回0,这意味着所有的比较都失败了。 我不明白为什么,有人能帮帮我吗?

这是C的一个好的子系统吗?

另请参见C Tokenizer 这是我写的C的快速substr()(是的,变量初始化需要移动到函数的开始等,但你明白了) 我已经看到很多substr()的“智能”实现,简单的一行调用strncpy()! 它们都是错的(strncpy不保证空终止,因此调用可能不会产生正确的子字符串!) 这可能更好吗? 带出虫子! char* substr(const char* text, int nStartingPos, int nRun) { char* emptyString = strdup(“”); /* C’mon! This cannot fail */ if(text == NULL) return emptyString; int textLen = strlen(text); –nStartingPos; if((nStartingPos < 0) || (nRun <= 0) || (textLen == 0) || (textLen < nStartingPos)) return emptyString; char* returnString […]

如何SubString,限制使用C?

第1号 #include #include #include #include int main(int argc, char **argv) { static const unsigned char text[] = “000ßh123456789”; int32_t current=1; int32_t text_len = strlen(text)-1; ///////////////////////////////// printf(“Result : %s\n”,text); ///////////////////////////////// printf(“Lenght : %d\n”,text_len); ///////////////////////////////// printf(“Index0 : %c\n”,text[0]); printf(“Index1 : %c\n”,text[1]); printf(“Index2 : %c\n”,text[2]); printf(“Index3 : %c\n”,text[3]);//==> why show this ` `? printf(“Index4 : %c\n”,text[4]);//==> why show […]

在较大的字符串中查找子字符串的位置

我创建了一个函数,该函数应该在较大的字符串中找到子字符串的第一个字符的数字位置。 我输出有一些问题,我不太清楚为什么。 这些问题包括每次返回-1而不是子串的整数位置。 我已经调试过,无法追踪function出错的地方。 这是函数应该执行的方式:如果我的字符串是“狗很快”而我正在搜索子字符串“dog”,则该函数应返回4.感谢chqrlie对循环的帮助。 这是function: int findSubString(char original[], char toFind[]) { size_t i, j; int originalLength = 0; int toFindLength = 0; originalLength = strlen(original) + 1; toFindLength = strlen(toFind) + 1; for (i = 0; i < toFindLength + 1; i++) { for (j = 0; j < originalLength + 1; j++) { […]

获取子字符串的索引

我有char * source ,我想要从它中提取subsrting,我知道它是从符号“abc”开始,并在源结束时结束。 使用strstr我可以得到poiner,但不是位置,没有位置我不知道子串的长度。 如何获得纯C中子字符串的索引?