Advertisement
Guest User

Untitled

a guest
Sep 18th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. typedef struct node_ {
  7.     char* val;
  8.     struct node_ * next;
  9. } node;
  10.  
  11. node *head = 0;
  12. node *curr = 0;
  13.  
  14. node *nodeWithString(char *string) {
  15.     node * out = malloc(sizeof(node));
  16.     out->val = string;
  17.     out->next = 0;
  18.     return out;
  19. }
  20.  
  21. void printList() {
  22.     curr = head;
  23.     while (curr != 0) {
  24.         if (curr->next == 0) {
  25.             printf("%s\n", curr->val);
  26.         } else {
  27.             printf("%s -> ", curr->val);
  28.         }
  29.         curr = curr->next;
  30.     }
  31. }
  32.  
  33. void insertNode(char *string) {
  34.         node *new = nodeWithString(strdup(string));
  35.         if (head == 0) {
  36.             head = new;  
  37.         } else {
  38.             curr = head;
  39.             while (curr->next != 0) {
  40.                 curr = curr->next;  
  41.             }
  42.             curr->next = new;
  43.         }
  44. }
  45.  
  46. int max(int one, int two) {
  47.     int out = one;
  48.     if (two > one) {
  49.         out = two;  
  50.     }
  51.     return out;
  52. }
  53.  
  54. int main(int argc, char** argv) {
  55.    
  56.     if (argc != 2) {
  57.         printf("ERROR: This program takes exactly one argument.\n");
  58.         return 1;
  59.     }
  60.        
  61.     int inputLength = strlen(argv[1]);
  62.    
  63.     char buffer[inputLength];
  64.     memset(buffer,0,inputLength); // Clear buffer
  65.            
  66.     int i;
  67.     for (i = 0; i < strlen(argv[1]); i++) {
  68.         char c = argv[1][i];
  69.         if (isalpha(c)) {
  70.             buffer[strlen(buffer)] = c; // Add character to buffer
  71.         } else {
  72.             insertNode(buffer);
  73.             memset(buffer,0,inputLength); // Clear buffer
  74.         }
  75.     }
  76.    
  77.     insertNode(buffer);
  78.    
  79.     printList();
  80.        
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement