Advertisement
wojtas626

[C] Zapamiętywanie łańcuchów znakowych

Jan 17th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6.  
  7. struct WordList
  8. {
  9.     char *word;
  10.     struct WordList *next;
  11. };
  12.  
  13.  
  14. void loadData(struct WordList **wordListPointer)
  15. {
  16.     char newWord[32];
  17.     (*wordListPointer)->next = NULL;
  18.     printf("Wprowadz wyraz: ");
  19.     scanf("%s", newWord);
  20.     (*wordListPointer)->word = (char*)malloc(sizeof(char) * strlen(newWord));
  21.     strcpy((*wordListPointer)->word, newWord);
  22. }
  23.  
  24.  
  25. void addWord(struct WordList **headPointer)
  26. {
  27.     struct WordList *pointer;
  28.  
  29.     pointer = *headPointer;
  30.     if (pointer == NULL)
  31.     {
  32.         pointer = (struct WordList*)malloc(sizeof(struct WordList));
  33.         loadData(&pointer);
  34.         *headPointer = pointer;
  35.     }
  36.     else
  37.     {
  38.         while(pointer->next != NULL)
  39.         {
  40.             pointer = pointer->next;
  41.         }
  42.         pointer->next = (struct WordList*)malloc(sizeof(struct WordList));
  43.         pointer = pointer->next;
  44.         loadData(&pointer);
  45.     }
  46. }
  47.  
  48.  
  49. char *convertToUpper(char *string)
  50. {
  51.     int stringLength, i;
  52.     char *newString;
  53.  
  54.     stringLength = strlen(string);
  55.     newString = (char*)malloc(sizeof(char)*(stringLength + 1));
  56.     for(i = 0; i < stringLength; i++)
  57.     {
  58.         newString[i] = toupper((int)string[i]);
  59.     }
  60.     return newString;
  61. }
  62.  
  63.  
  64. void findWords(struct WordList *headPointer, struct WordList **firstAlphabetically, struct WordList **lastAlphabetically)
  65. {
  66.     *firstAlphabetically = NULL;
  67.     *lastAlphabetically = NULL;
  68.  
  69.  
  70.     if (headPointer != NULL)
  71.     {
  72.         *firstAlphabetically = headPointer;
  73.         *lastAlphabetically = headPointer;
  74.  
  75.         while (headPointer->next != NULL)
  76.         {
  77.             headPointer = headPointer->next;
  78.             if (strcmp(convertToUpper((*firstAlphabetically)->word), convertToUpper(headPointer->word)) > 0)
  79.             {
  80.                 *firstAlphabetically = headPointer;
  81.             }
  82.             if (strcmp(convertToUpper((*lastAlphabetically)->word), convertToUpper(headPointer->word)) < 0)
  83.             {
  84.                 *lastAlphabetically = headPointer;
  85.             }
  86.         }
  87.     }
  88. }
  89.  
  90.  
  91. int main()
  92. {
  93.     struct WordList *head, *firstAlphabetically, *lastAlphabetically;
  94.     int numberOfWords, i;
  95.  
  96.  
  97.  
  98.     head = NULL;
  99.     printf("Wprowadz liczbe wyrazow: ");
  100.     scanf("%d", &numberOfWords);
  101.  
  102.     for (i = 0; i < numberOfWords; i++)
  103.     {
  104.         addWord(&head);
  105.     }
  106.  
  107.     findWords(head, &firstAlphabetically, &lastAlphabetically);
  108.  
  109.     printf("%s\n%s", firstAlphabetically->word, lastAlphabetically->word);
  110.  
  111.  
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement