voltage

strs

Nov 5th, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5.  
  6. typedef struct _line
  7. {
  8.   char data[80];
  9.   struct _line *next, *prev;
  10. } line;
  11.  
  12. /* squeeze spaces from string */
  13. void strsqueeze(char* str)
  14. {
  15.   char tmp[80];
  16.   size_t i, j = 0;
  17.  
  18.   for(i = strspn(str, " "); i < strlen(str); i++)
  19.   {
  20.     if(str[i] == ' ' && str[i + 1] == ' ')
  21.     {
  22.       continue;
  23.     }
  24.     tmp[j++] = str[i];
  25.   }
  26.   tmp[j] = '\0';
  27.   strcpy(str, tmp);
  28. }
  29.  
  30. /* get list element at rank */
  31. line* atrank(line* lines, const size_t rank)
  32. {
  33.   line* pt = lines->next;
  34.   size_t i;
  35.  
  36.   for(i = 0; i < rank; i++)
  37.   {
  38.     if(!pt)
  39.     {
  40.       return NULL;
  41.     }
  42.     pt = pt->next;
  43.   }
  44.   return pt;
  45. }
  46.  
  47. /* entry point */
  48. int main()
  49. {
  50.   line *lines, *pl, *pltmp;
  51.   size_t i = 0;
  52.   char *a, *b, c;
  53.  
  54.   lines = (line*)malloc(sizeof(line));
  55.   memset(lines, 0, sizeof(lines));
  56.  
  57.   /* read data */
  58.   pl = lines;
  59.   do
  60.   {
  61.     pltmp = (line*)malloc(sizeof(line));
  62.     memset(pltmp, 0, sizeof(pltmp));
  63.     pl->next = pltmp;
  64.     pltmp->prev = pl;
  65.     pl = pl->next;
  66.     gets(pltmp->data);
  67.   } while(!strstr(pltmp->data, "..."));
  68.  
  69.   /* squeeze spaces */
  70.   pl = lines;
  71.   while(pl)
  72.   {
  73.     strsqueeze(pl->data);
  74.     pl = pl->next;
  75.   }
  76.  
  77.   /* remove blanks */
  78.   pl = lines;
  79.   while(pl)
  80.   {
  81.     if(!strlen(pl->data))
  82.     {
  83.       if(pl->prev)
  84.       {
  85.         pl->prev->next = pl->next;
  86.       }
  87.       if(pl->next)
  88.       {
  89.         pl->next->prev = pl->prev;
  90.       }
  91.       pltmp = pl->next;
  92.       free((void*)pl);
  93.       pl = pltmp;
  94.     }
  95.     else
  96.     {
  97.       pl = pl->next;
  98.     }
  99.   }
  100.  
  101.   /* concatenate first and second strings */
  102.   strcat(atrank(lines, 0)->data, atrank(lines, 1)->data);
  103.  
  104.   /* copy the 7th string into the 5th */
  105.   strcpy(atrank(lines, 6)->data, atrank(lines, 4)->data);
  106.  
  107.   /* substutute lowercase letters after .!? to uppercase */
  108.   pl = lines;
  109.   while(pl)
  110.   {
  111.     a = pl->data;
  112.     while((i = strcspn(a, ".!?")) < strlen(a))
  113.     {
  114.       a += i;
  115.       while(!isalnum(*a))
  116.       {
  117.         a++;
  118.       }
  119.       *a = toupper(*a);
  120.     }
  121.     pl = pl->next;
  122.   }
  123.  
  124.   /* substutute chars from set by * in str #3 */
  125.   i = 0;
  126.   pl = atrank(lines, 2);
  127.   while(1)
  128.   {
  129.     i = strcspn(pl->data, "abc");
  130.     if(i >= strlen(pl->data))
  131.     {
  132.       break;
  133.     }
  134.     pl->data[i] = '*';
  135.   }
  136.  
  137.   /* swap case of the second rear entry of the specified character in str 4 */
  138.   pl = atrank(lines, 3);
  139.   a = strrchr(pl->data, 'a');
  140.   if(a)
  141.   {
  142.     c = *a;
  143.     *a = '\0';
  144.     b = strrchr(pl->data, 'a');
  145.     if(b)
  146.     {
  147.       if(islower(*b))
  148.       {
  149.         *b = toupper(*b);
  150.       }
  151.       else
  152.       {
  153.         *b = tolower(*b);
  154.       }
  155.     }
  156.     *a = c;
  157.   }
  158.  
  159.   /* print lines */
  160.   i = 0;
  161.   pl = lines->next;
  162.   while(pl)
  163.   {
  164.     printf("%d: %s\n", (int)i, pl->data);
  165.     pl = pl->next;
  166.     i++;
  167.   }
  168.  
  169.   /* free mem */
  170.   pl = lines;
  171.   while(pl)
  172.   {
  173.     pltmp = pl->next;
  174.     free((void*)pl);
  175.     pl = pltmp;
  176.   }
  177.  
  178.   return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment