Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<ctype.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5.  
  6. //strchr la tim kiem 1 ki tu trong 1 chuoi
  7. //neu tim thay thi tra ve sai
  8.  
  9. int isString(char s[]) {
  10. char regex[] = "@!#{}[])($%^&*";//thich k cho cai nao ki tu nao thi cho vao day
  11. for (int i = 0; i < strlen(regex); i++) {
  12. char* find = strchr(s, regex[i]);
  13. if (find != NULL) {
  14. printf("Do not allow enter special characters, Please re-input\n");
  15. return 0;
  16. }
  17. }
  18. return 1;
  19. }
  20.  
  21. //cat 1 choi ra mang 2 chieu khong bao gom dau cach
  22.  
  23. void split(char arr[][40], int &size, char s[]) {
  24. char *stok;
  25. size = 0;
  26. stok = strtok(s, " ");
  27. //tim dau cach de cat tat ca ca tu giong voi split trong java
  28. while (stok != NULL) {
  29. strcpy(arr[size++], stok);
  30. stok = strtok(NULL, " ");
  31. //khoi tao tiep cho bien chat
  32. }
  33. }
  34.  
  35. //noi cac tu rieng biet thanh 1 string co dau cach
  36.  
  37. void rebuild(char arr[][40], int size, char s[]) {
  38. char temp[40] = "";
  39. for (int i = 0; i < size - 1; i++) {
  40. strcat(temp, arr[i]);
  41. strcat(temp, " ");
  42. }
  43. strcat(temp, arr[size - 1]);
  44. strcpy(s, "");
  45. strcpy(s, temp);
  46. }
  47.  
  48. int isContinue() {
  49. char tmp;
  50. fpurge(stdin);
  51. printf("\nDo you want continue(press 0 exit): ");
  52. scanf("%c", &tmp);
  53. if (tmp == '0') {
  54. return 1;
  55. } else {
  56. return 0;
  57. }
  58. }
  59.  
  60. int main() {
  61. char string[60];
  62. char arr[30][40];
  63. int size = 0;
  64. do {
  65. do {
  66. fpurge(stdin);
  67. printf("Enter a string: ");
  68. gets(string);
  69. split(arr, size, string);
  70. rebuild(arr, size, string);
  71. } while (!isString(string));
  72. printf("%s", string);
  73. } while (!isContinue());
  74.  
  75. free(string);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement