Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<ctype.h>
- #include<string.h>
- #include<stdlib.h>
- //strchr la tim kiem 1 ki tu trong 1 chuoi
- //neu tim thay thi tra ve sai
- int isString(char s[]) {
- char regex[] = "@!#{}[])($%^&*";//thich k cho cai nao ki tu nao thi cho vao day
- for (int i = 0; i < strlen(regex); i++) {
- char* find = strchr(s, regex[i]);
- if (find != NULL) {
- printf("Do not allow enter special characters, Please re-input\n");
- return 0;
- }
- }
- return 1;
- }
- //cat 1 choi ra mang 2 chieu khong bao gom dau cach
- void split(char arr[][40], int &size, char s[]) {
- char *stok;
- size = 0;
- stok = strtok(s, " ");
- //tim dau cach de cat tat ca ca tu giong voi split trong java
- while (stok != NULL) {
- strcpy(arr[size++], stok);
- stok = strtok(NULL, " ");
- //khoi tao tiep cho bien chat
- }
- }
- //noi cac tu rieng biet thanh 1 string co dau cach
- void rebuild(char arr[][40], int size, char s[]) {
- char temp[40] = "";
- for (int i = 0; i < size - 1; i++) {
- strcat(temp, arr[i]);
- strcat(temp, " ");
- }
- strcat(temp, arr[size - 1]);
- strcpy(s, "");
- strcpy(s, temp);
- }
- int isContinue() {
- char tmp;
- fpurge(stdin);
- printf("\nDo you want continue(press 0 exit): ");
- scanf("%c", &tmp);
- if (tmp == '0') {
- return 1;
- } else {
- return 0;
- }
- }
- int main() {
- char string[60];
- char arr[30][40];
- int size = 0;
- do {
- do {
- fpurge(stdin);
- printf("Enter a string: ");
- gets(string);
- split(arr, size, string);
- rebuild(arr, size, string);
- } while (!isString(string));
- printf("%s", string);
- } while (!isContinue());
- free(string);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement