hqt

Workshop 8 - PFC

hqt
Jul 21st, 2012
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1. /*
  2.  * Name : Huynh Quang Thao
  3.  * Id : SE60963
  4.  * Description : Problem 1
  5.  */
  6. #include<stdio.h>
  7. #include<string.h>
  8.  
  9. int main() {
  10.  
  11.     printf("Word Counter\n");
  12.     printf("==============\n");
  13.     char s[100];
  14.     char tokenizer[] = {" \t\f\n\v"};
  15.     int count = 0;
  16.     while (1) {
  17.         fflush(stdin);
  18.         memset(s,0,sizeof(s));
  19.         count = 0;
  20.         printf("String to be counted:\n");
  21.         scanf("%[^\n]",s);
  22.         char * pch;
  23.         pch = strtok(s, tokenizer);
  24.         while (pch != NULL) {
  25.             count++;
  26.             pch = strtok(NULL, tokenizer);
  27.         }
  28.         printf("res:%d\n", count);
  29.     }
  30.  
  31.     return 0;
  32. }
  33.  
  34. /*
  35.  * Name : Huynh Quang Thao
  36.  * Id : SE60963
  37.  * Description : Problem 2
  38.  */
  39. #include <stdio.h>
  40. #include <string.h>
  41. int main(){
  42.     printf("String Cleaner\n");
  43.     printf("==============\n");
  44.     char s[100], d[100];
  45.     memset(d,0,sizeof(d));
  46.     char tokenizer[] = {" \t\f\n"};    
  47.     printf("String to be cleaned:"); scanf("%[^\n]",s);
  48.    
  49.     char * pch;
  50.     pch = strtok(s,tokenizer);
  51.     while (pch != NULL){
  52.         strncat(d,pch,strlen(pch));
  53.         strncat(d," ",1);
  54.         pch = strtok(NULL,tokenizer);
  55.     }
  56.     d[strlen(d)-1]='\0';
  57.     printf("Cleaned String:%s\n",d);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment