Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. // Define a structure to hold the characters
  6. typedef struct List_type
  7. {
  8. struct List_type *next;
  9. int value;
  10. } List;
  11.  
  12. int toLower = 0;
  13.  
  14. // Add new element to the list
  15. List* add_entry(int value, List *lastElement)
  16. {
  17. List *pTemp;
  18.  
  19. pTemp = malloc(sizeof(List)); // Allocate adequate memory
  20.  
  21. pTemp->value = value; // Insert the character
  22. pTemp->next = NULL; // Terminate the extended list
  23. lastElement->next = pTemp; // Link on the new item
  24.  
  25. return pTemp; // Return the new end value
  26. }
  27.  
  28. // Change characters
  29. int changeCharacter (int character)
  30. {
  31. if (isupper(character))
  32. {
  33. toLower++;
  34. return tolower(character);
  35. }
  36. else
  37. return toupper(character);
  38. }
  39.  
  40. // Method to print changed characters to standard output
  41. void printChangedText (List *fromEntry, int count)
  42. {
  43. putchar('\n');
  44. while (fromEntry != NULL)
  45. {
  46. putchar(changeCharacter(fromEntry->value));
  47. fromEntry = fromEntry->next;
  48. }
  49. putchar('\n');
  50. printf("Read %d characters in total,", count);
  51. printf(" %d converted to upper-case,", count-toLower);
  52. printf(" %d to lower-case\n", toLower);
  53. }
  54.  
  55. // Free the allocated
  56. void freeMemory (List *dummy)
  57. {
  58. while (dummy->next != NULL)
  59. {
  60. List *temp;
  61. temp = dummy->next;
  62. dummy->next = temp->next;
  63. free(temp);
  64. }
  65. }
  66.  
  67. int main(int argc, char **argv)
  68. {
  69. int noOfChar = 0;
  70. int current;
  71.  
  72. // ====================================================
  73. // Ask the user for input and output if there is any
  74. // ====================================================
  75. printf("Would you like to use an input file? (y/n)\n");
  76. int answer = getchar();
  77. // ====================================================
  78. // If the input is from a file
  79. // ====================================================
  80. if (answer == 'y')
  81. {
  82. char fileName[20];
  83. FILE *fileIn, *fileOut;
  84.  
  85. printf("What is the file name?\n");
  86. scanf("%s", fileName);
  87. if ((fileIn = fopen(fileName, "r")))
  88. {
  89. printf("Would you like to use an output file? (y/n)\n");
  90. fflush(stdin);
  91. int answer2 = getchar();
  92. // ====================================================
  93. // If there is an output file too
  94. // ====================================================
  95. if (answer2 == 'y')
  96. {
  97. printf("What is the file name?\n");
  98. scanf("%s", fileName);
  99.  
  100. fileOut = fopen(fileName, "w+");
  101.  
  102. current = fgetc(fileIn);
  103. while (current != EOF)
  104. {
  105. noOfChar++;
  106. fputc(changeCharacter(current), fileOut);
  107. current = fgetc(fileIn);
  108. }
  109. fputc('\n', fileOut);
  110. fprintf(fileOut, "Read %d characters in total,", noOfChar);
  111. fprintf(fileOut, " %d converted to upper-case,", noOfChar-toLower);
  112. fprintf(fileOut, " %d to lower-case\n", toLower);
  113. fclose(fileIn);
  114. fclose(fileOut);
  115. }
  116. else if (answer2 == 'n')
  117. {
  118. current = fgetc(fileIn);
  119. while (current != EOF)
  120. {
  121. noOfChar++;
  122. putchar(changeCharacter(current));
  123. current = fgetc(fileIn);
  124. }
  125. putchar('\n');
  126. printf("Read %d characters in total,", noOfChar);
  127. printf(" %d converted to upper-case,", noOfChar-toLower);
  128. printf(" %d to lower-case\n", toLower);
  129. fclose(fileIn);
  130. }
  131. else
  132. {
  133. printf("Answer does not exist. Please try again. %d\n", answer2);
  134. exit(1);
  135. }
  136. }
  137. else
  138. printf("Sorry, the file does not exist\n");
  139. exit(1);
  140. }
  141. else if (answer == 'n')
  142. {
  143. printf("Please type your text\n");
  144. // Last element
  145. List *lastElement;
  146. // Add a dummy entry as a start of the list
  147. List dummy;
  148. dummy.value = -1;
  149. dummy.next = NULL;
  150. lastElement = &dummy; // Set dummy as last element of the list
  151.  
  152. current = getchar(); // Read the first character
  153.  
  154. while (current != EOF) // Investigate the character != EOF
  155. {
  156. lastElement = add_entry(current, lastElement);
  157. noOfChar++;
  158. current = getchar();
  159. }
  160.  
  161. printChangedText(dummy.next, noOfChar);
  162. freeMemory (&dummy);
  163. }
  164. else
  165. {
  166. printf("Answer does not exist. Please try again.\n");
  167. exit(1);
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement