Tag: 字符串

将String拆分为String数组

我一直在为arduino编程,但今天我遇到了一个问题,我无法用我非常有限的C知识来解决。 这是怎么回事。 我正在创建一个将串行输入发送到arduino(deviceID,command,commandparameters)的pc应用程序。 这个arduino将把这个命令通过RF发送给其他arduino。 根据deviceID,正确的arduino将执行命令。 为了能够确定我想在“,”上拆分该字符串的deviceID。 这是我的问题,我知道如何在java中轻松地做到这一点(即使不使用标准的分割function),但在C中它是一个完全不同的故事。 你们中的任何人都能告诉我如何让这个工作吗? 谢谢 /* Serial Event example When new serial data arrives, this sketch adds it to a String. When a newline is received, the loop prints the string and clears it. A good test for this is to try it with a GPS receiver that sends out NMEA […]

字符串不是从C函数中的返回值捕获的

我有一个函数,它应该合并2个字符串并返回结果更长的字符串。 该字符串似乎已成功合并到函数中,但返回时信息将丢失。 函数中的’output’包含合并的字符串,应返回此值。 mergedString不在main中打印,而是在函数中打印。 为什么? 如何使其工作? main () { char *mergedString = mergeStrings( frags[maxOverlapArrPos1], frags[maxOverlapArrPos2], maxCharOverlap); printf (“mergedStringInMain is %s\n”, mergedString); } char * mergeStrings(char * string1, char * string2, int overlapCharSize){ int overlapStartPosition = strlen(string1) – overlapCharSize; char output[] = “”; if (strlen(string2)>overlapCharSize){ strncat(output, string1, overlapStartPosition); strncat(output , string2, strlen(string2)); } printf (“mergedStringInFunction is […]

替换字符串malloc中的字符

给定一个字符串和两个字符,我想找到第一个字符存在于字符串上的次数,它首先出现在什么位置并创建一个新字符串,其中第二个字符每次显示时都替换第一个字符,但我是最后一部分有问题。 这是我到目前为止: #include #include #include main () { char string[10], string1; char c1, c2; int contador, i, l, n, contador2; printf (“Introduza uma string e dois caracteres.\n”); scanf (“%s %c %c”, string, &c1, &c2); l = strlen (string); contador = 0; for (n = 0; n < l; n++) { if (c1 == string[n]) { […]

如何在C中按升序对字符串数组进行排序

问题 我已经制作了与其他类似的排序程序 C Program to Sort set of strings in alphabetical order 但我做的程序不起作用。 我认为两者都是一样的,但我的计划给了我废物输出。 另外我想知道其他程序计数设置为5例如它应该从0开始输入6但是它只获得5,如何? 我的计划 #include #include int main() { char str[4][10],temp[10]; int i,j; printf(“Enter strings one by one : \n”); for(i=0;i<5;i++) scanf("%s",str[i]); for(i=0;i<5;i++) for(j=i+1;j0){ strcpy(temp,str[i]); strcpy(str[i],str[j]); strcpy(str[j],temp); } printf(“\nSorted List : “); for(i=0;i<5;i++) printf("\n%s",str[i]); printf("\n\n"); return 0; }

在C编程中将2个数组复制到1个数组中

如何将2个单独的2D数组复制到1个数组中,我在下面描述了我的意思: 我有一个数组:a,b,c我有第二个数组:d,e,f 我希望第三个数组同时具有上述数组:第三个数组:a,b,c,d,e,f 到目前为止,我的代码只是获取两个数组的值,并在打印第三个数组时注释掉: #include #include #include int main(){ int i,j,count; char ar1[3][10]={“a”,”b”,”c”}; char ar2[3][10]={“d”,”e”,”f”}; char ar3[6][10]; for (i=0;i<3;i++){ printf("%s\n",ar1[i]); } for (i=0;i<3;i++){ printf("%s\n",ar2[i]); } printf('new array:\n'); // for (i=0;i<6;i++) // printf("%s\t\n",ar3[i]); }

在C中拆分未加引号的字符串

我正在编写一个函数将字符串拆分为指向指针的指针,如果separator是空格,我想只拆分不在引号内的单词。 例如Hello world “not split”应该返回 Hello world “not split” 不知何故,该函数将引号内的单词拆分,并且不会在引号之外拆分单词。 #include #include #include int is_quotes(char *s) { int i; int count; i = 0; count = 0; while (s[i]) { if (s[i] == ‘”‘) count++; i++; } if (count == 0) count = 1; return (count % 2); } int count_words(char *s, char sep) { […]

将字符串从文件存储到数组

我已经从两个文件中读取了字符串并将它们存储到两个独立的数组中,然后我尝试对它们进行排序并将它们写入另一个文件中…这部分按照它应该的方式工作,但我真正需要做的是组合字符串从两个数组到一个,以便我可以按字母顺序对它们进行排序,然后写入文件,但问题是,当我尝试将这两个数组存储到一个数组中时,我得到分段错误作为错误消息。我真的不知道如何将这两个数组存储到另一个数组,以便我可以按顺序排序…我知道如何排序它我只是不知道如何将这些读入另一个数组…我不能使用#DEFINE因为我将把所有这些写成测试代码的函数… 我尝试过类似的东西 new [i] = str; 并且: 的strcpy(新[I],STR) 但这些都不起作用……我们将非常感谢任何帮助…… 这是我的代码: #include #include main (void) { char str[200]; char str2[300]; char new[300]; char temp [300]; int linenumber=0; FILE *fa = fopen (“book1.dat”, “r”); FILE *fb = fopen (“book2.dat”, “r”); FILE *fc = fopen (“fixed.txt”, “w”); int i=0; int j=0; int k; /*read the strings into an […]

如何检查字符串是否在文件中

我是C的新手,我正在试图弄清楚如何在一个包含多个数字和单词的文件中搜索特定字符串及其后面的数字。 我正在使用的输入文件如下所示: TREES 2 BENCHES 5 ROCKS 10 PLANTS 8 我知道如何使函数读取字符串,我知道如何比较两个字符串但我不知道如何将它们放在一起或如何设置数组以读取整个文件。 我一直在使用strcmp ,但我只是不知道如何初始化我想要寻找的这个词。

如何读取一系列空格分隔的整数,直到遇到换行符?

我一直在尝试编写一个程序来读取一系列空格分隔的整数,直到遇到换行符。我的方法是将输入作为字符串读取并使用atoi()将字符串转换为整数。 这是我的方法: #include #include #include int main() { int a[100],i=0,k=0; char s[100]; //Read the first character scanf(“%c”,&s[i]); //Reads characters until new line character is encountered while(s[i]!=’\n’){ i+=1; scanf(“%c”,&s[i]); } //Print the String printf(“\nstring = %s\n”,s); //Trying to convert the characters in the string to integer for(i=0;s[i]!=’\0′;i++){ if(isdigit(s[i])) { a[k] = atoi(s); k+=1; } } //Printing […]

不使用atoi将String转换为Integer

如果不使用给定参数的atoi,我如何将字符串转换为整数? 这是我尝试过的: int main(int argc, char *argv[]){ for(int i = 1; i < argc; i++){ const char *p = argv[i]; int j = 0; while(isdigit(*p)){ j = j * 10 + *p – '0'; p++; printf("%d\n", j); } } } 出于某种原因,它正在拆分并再次重新添加它们。 $ ./a.out 55 6 50 66 5 55 6 5 50 6 66