字符串两头堵的模型就是去掉两边空格,已经字符串1,在字符串2出现的次数,不修改输入。
2014-04-27 21:52
441 查看
主调函数分配内存块;在被调用函数中使用; //写一个函数,;写一个主函数,用被调用函数,求出字符串1.在字符串2中间出现过几次! #define _CRT_SECURE_NO_WARNINGS #include"stdio.h" #include"stdlib.h" #include"string.h" char mycount(char *myp, char *mysub, int *n) { int ret = 1; if (myp == NULL || mysub == NULL||n==NULL) { ret = -1; printf("func mycount() err %d (myp == NULL || mysub == NULL||n==NULL) ",ret); return ret; } char *Myp = myp; char *Mysub = mysub; do { Myp = strstr(Myp, Mysub); if (Myp != NULL) { (*n)++; //要把所在地址的值取出来++ Myp = Myp + strlen(Mysub); } else { break; } } while (*Myp != '\0'); return ret; } void main() { char *sub = "abcd"; int n = 0; char *p = "adiabcd11122abcdqqqabcd1"; mycount(p, sub, &n);//传入函数; printf("一共出现几次%d",n); system("pause"); } 在写一个内存模型两头堵 #define _CRT_SECURE_NO_WARNINGS #include"stdio.h" #include"stdlib.h" #include"string.h" #include<ctype.h> /* 有一个字符串开头或结尾含有n个空格(" abcdefgddddd "),欲去掉前后空格,返回一个新字符串; 要求1:请自己定义一个接口(函数),并实现功能; 要求2;编写测试用例; int trimspace(char *inbuf,char *outbuf) */ //不调用函数; void main() { int count=0; int i = 0; int j = 0; char p[40] = " abcd "; char p2[40]; j = strlen(p) -1;// 算出有下标 -1 是因为 下标从0开始。 while (isspace(p[i]) && p[i] != '\0') { i++; } while (isspace(p[j]) && p[j] != '\0') { j--; } count = j - i+1;//因为下标从0开始 所以当然少了一个。所以要加上1 //mencpy 从第二个内存首地址开始,拷贝数据拷到前面第一个内存,大小烤N, 返回出来是第一个内存块的首address memcpy(p2, p+i, count); //里面千万不能写p[i]这个小错。我找了大半天的。他妹的。 p2[count] = '\0'; printf("\n%s",p2); system("pause"); } //调用函数 /* int trimSpace_better( const char *myBuf, char *outBuf) { int retVal = 0; //指针变量为空,返回-1; if (myBuf == NULL || outBuf==NULL) { return -1; } //指针变量变量不为空,但是指针指向的字符串为空的,就返回-2 const char *p = myBuf; int i = 0; int j = strlen(p) - 1; while (isspace(p[i]) && p[i] != '\0') { i++; } while (isspace(p[j]) && p[j] >=0) { j--; } int count = j - i + 1;//因为下标从0开始 所以当然少了一个。所以要加上1 memcpy(outBuf, p + i, count); outBuf[count] = '\0'; return retVal; } int main() { int retVal = 0; char buf[100] = " abcd1111cdeadsf "; char bufOut[100]; //char *p=" abcd1111cdeadsf "; retVal = trimSpace_better(buf, bufOut); if (retVal != 0) { printf("func trimSpace()err ;%d ", retVal); return 0; } printf("%s\n", bufOut); system("pause"); } */
相关文章推荐
- C#练习——去掉字符串两端的空格、统计字符出现的次数及出现的索引位置、随机生成10个1-100之间的不重复的偶数
- 输入等号前面的字符串,出现后面的字符串。并把空格去掉
- [学习]OC-NSString去掉两边的空格,查找字符串出现的位置,获取远程URL 内容
- 两头堵模型:有一个字符串开头或结尾有n个空格,欲去掉前后空格返回一个新的字符串
- C++输入一个字符串,去掉这个字符串中出现次数最少的字符 例如: 输入:abcabbc 输出:bbb
- 手动输入一个字符串,仅限小写字母,统计并输出每个字符在字符串中出现的次数,并输出。
- 【C语言】编写一个程序统计输入字符串中: 各个数字、空白字符、以及其他所有字符出现的次数。
- 通过键盘输入一个字符串,判断字符串中出现最多的字符并输出该字符和输出出现次数
- 去掉字符串两边空格
- 去掉字符串左右两边的空格
- java----输入一个字符串,去掉字符串开头的空格和结尾的空格,若字符串中间有连续的空格,则只保留一个空格。
- java----输入一个字符串,去掉字符串开头的空格和结尾的空格,若字符串中间有连续的空格,则只保留一个空格。(使用list解答)
- 输入一个字符串,计算字符串中子串出现的次数
- 输入一个字符串,删除出现次数最少的字符串。
- 错了两次,就是知先序和中序求后序的,但是不能用字符串,因为输入中间有空格,输出也有空格
- 对于给定的一个字符串,统计其中数字字符出现的次数。输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。
- JS去掉字符串两边(左右)的空格(js去空格)
- 输入一个字符串,计算字符串中子串出现的次数
- Java - 写一个方法,输入一个文件名和一个字符串,统计这个字符串在这个文件中出现的次数。
- 去掉输入字符串中间的空格