Advertisement
mtrangg

ws7 đáng ghét

Apr 4th, 2020
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <windows.h>
  5.  
  6. void input(char s[]){
  7.     printf("\nInput a string: ");
  8.     fflush(stdin);
  9.     gets(s);
  10. }
  11. //HAM DEM XEM BAO NHIEU TU TRONG CHUOI
  12. int countWord(char s[]){
  13.     int word=0;
  14.     for(int i=0; i<strlen(s); i++)
  15.         if(s[i]!=32&&(s[i+1]==32||s[i+1]==NULL)) word++;
  16.     return word;
  17. }
  18. //HAM CAT CHUOI
  19. void cutWord(char s[]){
  20.     int count=0;
  21.     char p[100]; strcpy(p,s);
  22.     printf("The original string: ");
  23.     puts(s);
  24.     printf("Number of words in the string: %d, they are: \n",countWord(s));
  25.     char *token;
  26.     token=strtok(p," \t");
  27.     while(token!=NULL){
  28.         puts(token);
  29.         token=strtok(NULL," \t");
  30.         count++;
  31.     }
  32. }
  33. //HAM VIET HOA DAU TU
  34. void upperWord(char s[]){
  35.     char p[100]; strcpy(p,s);
  36.     printf("The original string: ");
  37.     puts(s);
  38.     printf("The proper form: ");
  39.     for(int i=0; i<=strlen(p); i++){
  40.         if(p[i]==32&&p[i+1]!=32){
  41.             if(p[i+1]>='a'&&p[i+1]<='z') p[i+1]-=32;
  42.         }
  43.         if(p[0]>='a'&&p[0]<='z') p[0]-=32;
  44.     }
  45.     puts(p);
  46. }
  47. //HAM XOA KHOANG CACH
  48. void deleteSpace(char s[]){
  49.     char p[100]; strcpy(p,s);
  50.     printf("The original string: ");
  51.     puts(s);
  52.     printf("The cleaned form: ");
  53.     for(int i=0; i<strlen(p); i++){
  54.         if(p[i]==32&&p[i+1]==32){
  55.             for(int j=i; j<strlen(p); j++)
  56.                 p[j]=p[j+1];
  57.             i--;   
  58.             p[strlen(p)]=NULL;
  59.         }
  60.     }
  61.     if(p[0]==32){
  62.         for(int i=0; i<strlen(p); i++)
  63.             p[i]=p[i+1];
  64.         s[strlen(p)]=NULL;
  65.     }
  66.     if(s[strlen(p)]==32)
  67.         s[strlen(p)]=NULL;
  68.     puts(p);
  69. }
  70. //HAM CHECK TEN
  71. void checkName(char s[]){
  72.     bool check=true;
  73.     for(int i=0; i<strlen(s); i++){
  74.         if(isalpha(s[i])==0&&s[i]!=32){
  75.             check=false;
  76.             break;
  77.         }  
  78.     }
  79.     if(check==true) printf("%s is a valid name\n",s);
  80.     else printf("%s is not a valid name\n",s);
  81. }
  82. int main(){
  83.     char s[100];char next;
  84.    
  85.     int selection;
  86.     do{
  87.         system("cls");
  88.         printf("Choose one of the following options:\n");
  89.         printf("1. Input a string\n");
  90.         printf("2. Split a string into words\n");
  91.         printf("3. Convert a string into proper form\n");
  92.         printf("4. Remove redundant spaces\n");
  93.         printf("5. Check valid name\n");
  94.         printf("0. Exit\n");
  95.         printf("Your selection (0->6): ");
  96.         scanf("%d", &selection);
  97.         switch(selection){
  98.             case 1: input(s); break;
  99.             case 2: cutWord(s); break;
  100.             case 3: upperWord(s); break;
  101.             case 4: deleteSpace(s); break;
  102.             case 5: checkName(s);break;
  103.             case 0: return 0;
  104.         }
  105.         printf("Enter y/n: ");fflush(stdin);scanf("%c",&next);
  106.     }while(next=='y');
  107.    
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement