Tboy Cá voi
Tổng số bài gửi : 178 Cảm ơn : 413 Danh vọng : 4 Join date : 03/01/2011 Age : 32 Đến từ : TPHCM
| Tiêu đề: Chèn ký tự - (Chuỗi - Xâu ký tự) Mon Mar 07, 2011 9:55 pm | |
| | Mon Mar 07, 2011 9:55 pm Chèn ký tự - (Chuỗi - Xâu ký tự) | | | Chèn kí tự: - Code:
-
#include <stdio.h> #include <conio.h> #include <string.h> #include <malloc.h> char *Chen(char *S,int K, char C); void main() { char *s,c; int k; printf("Nhap chuoi: "); flushall(); gets(s); int len=strlen(s); printf("Nhap ki tu can chen: "); scanf("%c",&c); do { printf("Nhap vi tri can chen: "); scanf("%d",&k); }while(k<1||k>len+1); printf("Sau khi chen: %s\n",Chen(s,k-1,c)); getch(); } char *Chen(char *S,int K, char C) { char *t; int len = strlen(S); t=(char *)malloc((len+1)*sizeof(char)); strcpy(t,S); memcpy(t+K+1,t+K,len-K+1); t[K]=C; return t; } Chèn chuỗi: - Code:
-
#include <stdio.h> #include <conio.h> #include <string.h> #include <malloc.h> #define Max 100 char *Chen(char *S,int K, char *C); void main() { char *s,*c; int k; printf("Nhap chuoi: "); flushall(); gets(s); int len=strlen(s); flushall(); printf("Nhap chuoi can chen: "); gets(c); do { printf("Nhap vi tri can chen: "); scanf("%d",&k); }while(k<1||k>len+1); printf("Sau khi chen: %s\n",Chen(s,k-1,c)); getch(); } char *Chen(char *S,int K, char *C) { char *t; int len1 = strlen(S),len2= strlen(C); t=(char *)malloc((len1+len2)*sizeof(char)); strcpy(t,S); memcpy(t+K+len2,t+K,len1-K+1); memcpy(t+K,C,len2); return t; } Tboy
| | | | | |
|