Yêu cầu: Thay thế kí tự space trong chuỗi bằng kí tự khác
Thuật toán: duyệt từng phần tử và kiểm tra nếu là kí tự sapce, thay bằng kí tự khác.
/***************Replace space*********************** * Author: vncoding * Date : 11/01/2016 ***************************************************************/ #include "stdio.h" #include "conio.h" #include "string.h" void repstr(char *str, char c); void main() { char str[] = "vn coding forum "; char c = '+'; printf("original string: '%s'\n", str); repstr(str, c); printf("new string: '%s'\n", str); getch(); } /************************************************ Replace space by character c *************************************************/ void repstr(char *str, char c) { char *p = str; while(*p != NULL) { if (*p == ' ' || *p == '\t') { *p = c; } p++; } }
Kết quả:
Leave a Reply
You must be logged in to post a comment.