用指针分割字符串的问题!!!!!
一个字符中有若干%,按%分割,怎么作,不能用系统函数,只能用指针!!!还要打印出%个数和分割的字符串内容!
#include <stdio.h> #include <stdlib.h> #define MAX_BUFFER 18 int main(void) { char *buffer = NULL; char *temp[28] = {NULL}; int count = 0; int zi_count = 0; int count2 = 0; int size_temp = 0; int i = 0; int j = 0; buffer = (char*)malloc(MAX_BUFFER); while((buffer[count] = getchar()) !='\n') { if(buffer[count] == '%') { zi_count++; count2 = count; /*读取上一个单词*/ while(buffer[--count2] != '%' && count2>=0) size_temp++; temp[i] = (char*)malloc(size_temp+1); while(buffer[++count2] != '%') temp[i][j++] = buffer[count2]; temp[i++][j] = '\0'; j = 0; } count++; } /*输出*/ for(j=0;j<i;j++) printf("%s\n",temp[j]); printf("Have %d of %c\n",zi_count,'%'); return 0; }
#include <stdio.h> #include <stdlib.h> #define MAX_BUFFER 30 int main(void) { char *buffer = NULL; int count = 0; int zi_count = 0; int i = 0; buffer = (char*)malloc(MAX_BUFFER); while((buffer[count] = getchar()) !='\n') { if(buffer[count] == '%') { buffer[count] = '\n'; zi_count++; } count++; } buffer[count] = '\0'; /*输出*/ printf("%s\n",buffer); printf("Have %d of %c\n",zi_count,'%'); return 0; }
#include <stdio.h> #include <conio.h> unsigned int Section(const char* s, char delimiter); void Pause(void); void main(void) { const char delimiter = '%'; printf_s("\nthe %c count is %u\n", delimiter, Section("abc%fdfd% dfro9 043cv %dfdldsfd", delimiter)); Pause(); } unsigned int Section(const char* s, char delimiter) { unsigned int count = 0; while (*s != '\0') { if (*s != delimiter) { putchar(*s); } else { ++count; putchar('\n'); } ++s; } putchar('\n'); return count; } void Pause(void) { printf_s("Press any key to return..."); _getch(); }