Advertisement
pdaogu

HW13.1

Nov 27th, 2018
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <conio.h>
  5.  
  6. #define MAXSIZE 1000
  7.  
  8. void TrimLeft (char s[]) {
  9.     char *p = s;
  10.     while (isspace(*p))
  11.         ++p;
  12.     strcpy(s, p);
  13.     return;
  14. }
  15.  
  16. void TrimRight (char s[]) {
  17.     int i = strlen(s) - 1;
  18.     while (isspace(s[i]))
  19.         --i;
  20.     s[i+1] = '\0';
  21.     return;
  22. }
  23.  
  24. void TrimMiddle (char s[]) {
  25.     int i = 0, n = strlen(s), numSpace;
  26.     // if (isspace(s[0]))
  27.         // numSpace = 1;
  28.     // else
  29.         // numSpace = 0;
  30.     for (i = 1; i <= n; ++i) {
  31.         if (isspace(s[i]) && isspace(s[i-1])) {
  32.             ++numSpace;
  33.         } else {
  34.             s[i-numSpace] = s[i];
  35.         }
  36.     }
  37.     // if (isspace(s[n-numSpace-1]))
  38.         // s[n-numSpace-1] = '\0';
  39.     // Must use TrimRight because have a trailing space
  40. }
  41.  
  42. void Std (char s[]) {
  43.     int i;
  44.     for(i=0;i<strlen(s);i++)
  45. {
  46. if(s[i]==' '&&s[i+1]==' ') {
  47.     strcpy(&s[i],&s[i+1]);
  48.     i--;
  49.     //printf("@%s@\n", s);
  50. }
  51. }
  52. }
  53.  
  54. int main () {
  55.     char s[MAXSIZE];
  56.     printf("Enter a string:\n");
  57.     scanf("%[^\n]", s);
  58.     //Std(s);
  59.     TrimLeft(s);
  60.     printf("The string after trim left is:\n%s\n", s);
  61.     TrimRight(s);
  62.     printf("The string after trim right is:\n%s\n", s);
  63.     TrimMiddle(s);
  64.     printf("The string after trim middle is:\n%s\n", s);
  65.     getch();
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement