Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- typedef struct _line
- {
- char data[80];
- struct _line *next, *prev;
- } line;
- /* squeeze spaces from string */
- void strsqueeze(char* str)
- {
- char tmp[80];
- size_t i, j = 0;
- for(i = strspn(str, " "); i < strlen(str); i++)
- {
- if(str[i] == ' ' && str[i + 1] == ' ')
- {
- continue;
- }
- tmp[j++] = str[i];
- }
- tmp[j] = '\0';
- strcpy(str, tmp);
- }
- /* get list element at rank */
- line* atrank(line* lines, const size_t rank)
- {
- line* pt = lines->next;
- size_t i;
- for(i = 0; i < rank; i++)
- {
- if(!pt)
- {
- return NULL;
- }
- pt = pt->next;
- }
- return pt;
- }
- /* entry point */
- int main()
- {
- line *lines, *pl, *pltmp;
- size_t i = 0;
- char *a, *b, c;
- lines = (line*)malloc(sizeof(line));
- memset(lines, 0, sizeof(lines));
- /* read data */
- pl = lines;
- do
- {
- pltmp = (line*)malloc(sizeof(line));
- memset(pltmp, 0, sizeof(pltmp));
- pl->next = pltmp;
- pltmp->prev = pl;
- pl = pl->next;
- gets(pltmp->data);
- } while(!strstr(pltmp->data, "..."));
- /* squeeze spaces */
- pl = lines;
- while(pl)
- {
- strsqueeze(pl->data);
- pl = pl->next;
- }
- /* remove blanks */
- pl = lines;
- while(pl)
- {
- if(!strlen(pl->data))
- {
- if(pl->prev)
- {
- pl->prev->next = pl->next;
- }
- if(pl->next)
- {
- pl->next->prev = pl->prev;
- }
- pltmp = pl->next;
- free((void*)pl);
- pl = pltmp;
- }
- else
- {
- pl = pl->next;
- }
- }
- /* concatenate first and second strings */
- strcat(atrank(lines, 0)->data, atrank(lines, 1)->data);
- /* copy the 7th string into the 5th */
- strcpy(atrank(lines, 6)->data, atrank(lines, 4)->data);
- /* substutute lowercase letters after .!? to uppercase */
- pl = lines;
- while(pl)
- {
- a = pl->data;
- while((i = strcspn(a, ".!?")) < strlen(a))
- {
- a += i;
- while(!isalnum(*a))
- {
- a++;
- }
- *a = toupper(*a);
- }
- pl = pl->next;
- }
- /* substutute chars from set by * in str #3 */
- i = 0;
- pl = atrank(lines, 2);
- while(1)
- {
- i = strcspn(pl->data, "abc");
- if(i >= strlen(pl->data))
- {
- break;
- }
- pl->data[i] = '*';
- }
- /* swap case of the second rear entry of the specified character in str 4 */
- pl = atrank(lines, 3);
- a = strrchr(pl->data, 'a');
- if(a)
- {
- c = *a;
- *a = '\0';
- b = strrchr(pl->data, 'a');
- if(b)
- {
- if(islower(*b))
- {
- *b = toupper(*b);
- }
- else
- {
- *b = tolower(*b);
- }
- }
- *a = c;
- }
- /* print lines */
- i = 0;
- pl = lines->next;
- while(pl)
- {
- printf("%d: %s\n", (int)i, pl->data);
- pl = pl->next;
- i++;
- }
- /* free mem */
- pl = lines;
- while(pl)
- {
- pltmp = pl->next;
- free((void*)pl);
- pl = pltmp;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment