Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctype.h>
- #include <stdio.h>
- #include <stdlib.h>
- // Define a structure to hold the characters
- typedef struct List_type
- {
- struct List_type *next;
- int value;
- } List;
- int toLower = 0;
- // Add new element to the list
- List* add_entry(int value, List *lastElement)
- {
- List *pTemp;
- pTemp = malloc(sizeof(List)); // Allocate adequate memory
- pTemp->value = value; // Insert the character
- pTemp->next = NULL; // Terminate the extended list
- lastElement->next = pTemp; // Link on the new item
- return pTemp; // Return the new end value
- }
- // Change characters
- int changeCharacter (int character)
- {
- if (isupper(character))
- {
- toLower++;
- return tolower(character);
- }
- else
- return toupper(character);
- }
- // Method to print changed characters to standard output
- void printChangedText (List *fromEntry, int count)
- {
- putchar('\n');
- while (fromEntry != NULL)
- {
- putchar(changeCharacter(fromEntry->value));
- fromEntry = fromEntry->next;
- }
- putchar('\n');
- printf("Read %d characters in total,", count);
- printf(" %d converted to upper-case,", count-toLower);
- printf(" %d to lower-case\n", toLower);
- }
- // Free the allocated
- void freeMemory (List *dummy)
- {
- while (dummy->next != NULL)
- {
- List *temp;
- temp = dummy->next;
- dummy->next = temp->next;
- free(temp);
- }
- }
- int main(int argc, char **argv)
- {
- int noOfChar = 0;
- int current;
- // ====================================================
- // Ask the user for input and output if there is any
- // ====================================================
- printf("Would you like to use an input file? (y/n)\n");
- int answer = getchar();
- // ====================================================
- // If the input is from a file
- // ====================================================
- if (answer == 'y')
- {
- char fileName[20];
- FILE *fileIn, *fileOut;
- printf("What is the file name?\n");
- scanf("%s", fileName);
- if ((fileIn = fopen(fileName, "r")))
- {
- printf("Would you like to use an output file? (y/n)\n");
- fflush(stdin);
- int answer2 = getchar();
- // ====================================================
- // If there is an output file too
- // ====================================================
- if (answer2 == 'y')
- {
- printf("What is the file name?\n");
- scanf("%s", fileName);
- fileOut = fopen(fileName, "w+");
- current = fgetc(fileIn);
- while (current != EOF)
- {
- noOfChar++;
- fputc(changeCharacter(current), fileOut);
- current = fgetc(fileIn);
- }
- fputc('\n', fileOut);
- fprintf(fileOut, "Read %d characters in total,", noOfChar);
- fprintf(fileOut, " %d converted to upper-case,", noOfChar-toLower);
- fprintf(fileOut, " %d to lower-case\n", toLower);
- fclose(fileIn);
- fclose(fileOut);
- }
- else if (answer2 == 'n')
- {
- current = fgetc(fileIn);
- while (current != EOF)
- {
- noOfChar++;
- putchar(changeCharacter(current));
- current = fgetc(fileIn);
- }
- putchar('\n');
- printf("Read %d characters in total,", noOfChar);
- printf(" %d converted to upper-case,", noOfChar-toLower);
- printf(" %d to lower-case\n", toLower);
- fclose(fileIn);
- }
- else
- {
- printf("Answer does not exist. Please try again. %d\n", answer2);
- exit(1);
- }
- }
- else
- printf("Sorry, the file does not exist\n");
- exit(1);
- }
- else if (answer == 'n')
- {
- printf("Please type your text\n");
- // Last element
- List *lastElement;
- // Add a dummy entry as a start of the list
- List dummy;
- dummy.value = -1;
- dummy.next = NULL;
- lastElement = &dummy; // Set dummy as last element of the list
- current = getchar(); // Read the first character
- while (current != EOF) // Investigate the character != EOF
- {
- lastElement = add_entry(current, lastElement);
- noOfChar++;
- current = getchar();
- }
- printChangedText(dummy.next, noOfChar);
- freeMemory (&dummy);
- }
- else
- {
- printf("Answer does not exist. Please try again.\n");
- exit(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement