wwfl.net
当前位置:首页 >> 函数F(ChAr *s,ChAr DEl),将非空字符串str分割成... >>

函数F(ChAr *s,ChAr DEl),将非空字符串str分割成...

#include <stdio.h>#include <string.h>void f(char *s,char del){ char buf[BUFSIZ] = {0}; char *p = buf; char *q = buf; strcpy(buf, s); while(*p) { if(del == *p) { *p = '\0'; if(*q) printf("%s\n", q); q = p + 1; } ++p; } if(*q) printf("%s\n", q); return ;}int main(void){ f("33123333435", '3'); return 0;}

#include <stdio.h>void f(char *s,char del){ int flag=0; while (*s==del ) s++;//去掉开始的 while ( *s ) { if ( *s == del ) { if ( flag==0 ) //防重复输出 { printf(" "); } flag ++ ; } else { flag=0; printf("%c", *s ); } s++ ; } printf("\n");}void main(){ char s[]="33123333435", ch='3'; f(s,ch);}

[图文] 函数f(char *str, char del)的功能是:将非空字符串str分割成若干个子字符串并输出,del表示分割时的标志字符. 例如,若str的值为“33123333435”,del的值为“3”,调用此函数后,将输出三个子字符

void chardel(char *s){ for(char *p = s; *(s-1);s++) if(*s!='k') *p++ = *s;}

关于这个问题,char*chardel(char*s){char*p=s;while(*p!='0'){if(*p=='k'){char*p1=p;while(*p1!='0'){*p1=*(p1+1);p1++;}}p++;}returns;}

实现思路:通过split方法,根据固定的分割规则实现分割.举例一:public class stringsplit { public static void main(string[] args) { string sourcestr = "1,2,3,4,5"; string[] sourcestrarray = sourcestr.split(","); for (int i = 0; i system.out.println(

#include void delchar(char *s, char c_h) { int len = (int)strlen(s); int count=0,i; while(*(s+count)!='\0') { if(*(s+count) == c_h) { for(i=count; i { *(s+i)=*(s+i+1); } len--; }else { count++; } } } int main() { char s[256],c; printf("input s c:\n"); scanf("%s %c",s,&c); delchar(s,c); printf("str:%s",s); }

错误在第8行:if(*(s+i)!=' ') {j++;*(s+j)=*(s+i);}j先加了,会成无限循环的.严重会segment fault换成:if(*(s+i)!=' ') {*(s+j)=*(s+i);j++;}另外strdel有问题,改成:int strdel(char *s){ int i,k=0,j=0; for(i=0;(k+j) 评论0 0 0

网站首页 | 网站地图
All rights reserved Powered by www.wwfl.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com