Tag: 字符串

在C中反转字符串

我尝试构建将反转字符串的代码,如下所示: input: Hello World! output:olleH !dlroW 我不能使用任何指针我构建的代码是,有一个函数,得到句子列表地址(矩阵与多达20行) – 表,表中的句子数和句子的数量我想改变。 int changeCharOrder(char table[][MAX_SENTENCE_LENGTH], int numOfSentences, int sentenceToChange) { int slen, slen2, slenrev, lastwordlen=0, c=0, i, q, f, start=0; char reversed[MAX_SENTENCE_LENGTH]; // the reversed sentence will be here char oneword[MAX_SENTENCE_LENGTH]; // i tried to split the sentence to words and then reverse them reversed[0] = ‘\0’; slen […]

realloc()泄漏内存

我有一个函数,它为字符串添加一个字符: void AddChToString(char **str,char ch){ int len=(*str)?strlen(*str):0; (*str)=realloc(*str, len+2); (*str)[len]=ch; (*str)[len+1]=’\0′; } 仪器(在mac上)和Valgrind表示行:(* str)= realloc(* str,len + 2)是泄漏内存。 这是realloc的实现问题吗? 或者我使用不当? 这是Valgrind的输出: ==39230== 6 bytes in 1 blocks are definitely lost in loss record 1 of 7 ==39230== at 0x100018B2D: realloc (vg_replace_malloc.c:525) ==39230== by 0x100002259: AddChToString (in ./OpenOtter) ==39230== by 0x10000477B: QueryMapFromString (in ./OpenOtter) ==39230== by […]

C双字符指针声明和初始化

我总是这样宣告 char *c = “line”; 和…一样 char c[] = “line”; 所以我做到了 char **choices = { “New Game”, “Continue Game”, “Exit” }; 这给了我一个不兼容的指针类型,在哪里 char *choices[] = { “New Game”, “Continue Game”, “Exit” }; 没有。 理解这个有什么帮助吗?

查找字符串中的子字符串数

下面的代码产生了一个荒谬的输出32674,用于’aaa’中计数’aa’的测试输入。 我该如何纠正这个? #include #include int main() { int i,j,len ,k ,count, num ; char str[100],sub[100],comp[100] ; // sub is the sub string . printf(“Please enter the string”) ; fgets(str,sizeof(str),stdin) ; printf(“Enter the substring to be searched for”) ; fgets(sub,sizeof(str),stdin) ; len=strlen(sub) ; for ( i=0 ; i < (strlen(str) – len ); i++ ) /*Goes […]

删除C中char数组中相同的连续行

我正在尝试创建一个函数来检测char数组中是否有相同的连续行。 例如,如果char数组包含: 你好 你好 你好 你好 你好 然后数组将更改为 你好 你好 基本上,我想检测连续的,相同的行,并删除它们,因此只剩下其中一行。 如果一行与早期行相同,但它们不是连续的,那么就没问题了。 实际上,整条线不必相同,但至少前79或MAXCHARS必须相同。 另外,我不想通过写入中间文件来做到这一点。 理想情况下,我会将数据存储在数组中。 我想的是: int deleteRepeats(char *a) { int i; for (i=0; i<=MAXCHARS; i++) { if (a[i] != '\n') /* copy into new array /* } } 但我有点失落。 我现在不想打印arrays,因为我将在以后的程序中再次修改它; 我仍然需要使用。 非常感谢任何帮助/解决方案。 谢谢。

你如何在C中连接字符串?

可能重复: 如何在C中连接2个字符串? #include #include /* Function prototypes */ void wordLength ( char *word ); void wordConcat ( char *wordC1, char *wordC2); int main (void) { int choice; char word [20]; char wordC1 [20]; char wordC2 [20]; printf( “Choose a function by entering the corresponding number: \n” “1) Determine if words are identical\n” “2) Count number […]

如何使用C生成所有可能的子序列,包括字符串的非连续子序列

例如, char str[20]; str=”ABCD”; 输出: 1 – A, B, C,D 2 – AB,AC, AD BC, BD, CD. 3 – ABC, ABD, BCD. 4 – ABCD. 子序列仅以从左到右的方式生成。 谢谢 :) #include #include #include int sub[10]; void next(int max, int length) { int pos = length – 1,a; //find first digit that can be increased while(pos >= 0) […]

如何在C中的另一个字符串中插入一个字符串

我需要在特定位置的另一个字符串中插入一个字符串。 这是一个简单的例子: char *a = “Dany S.”; char *b = “My name is *a , I come from … “; 所以,在字符串b代替*a我希望有Dany S. 怎么做 ?

指向C中的字符串?

char *ptrChar; 我知道ptrChar是指向char的指针。 指向字符串的命令是什么? 编辑:如果它是相同的(ptr到char与ptr到字符串) – 下面的变量定义代表什么? char (*ptr)[N]; 我糊涂了。

评估以字符串forms给出的算术表达式

我正在开发一个项目,我需要计算以字符串forms给出的算术表达式的值。 所以我选择使用的方式就是运行字符串直到符号乘法。 在此期间,我保留乘法字符串之前的数字。 如果在这些数字之前有符号,我会重置字符串。 最后,当我产生乘法符号时,我检查接下来会发生什么并保存到另一个字符串中。 最后,我计算结果。 依此类推,直到我解决练习(还有其他function可以解决其余的方程)。 我的问题是:乘法符号之前的数字不会重置,除了在乘法符号之后的数字未保存在额外字符串中之外,还会产生问题。 找到乘法符号并计算他的函数 – #include #include #include int main(void) { char str[10] = “3+1-4*6-7”; char str1[10]; char str2[10]; int i, p = 0, k = 0; for(i = 0; i < 10; i++) { str1[k] = str[i]; k++; if((str[k] == '-') | '+' | '/') { str1[0] = 0; […]