Don't like ads? PRO users don't see any ads ;-)
Guest

week6.c

By: a guest on Apr 26th, 2012  |  syntax: C  |  size: 1.91 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <string.h>
  5.  
  6. #define TRUE 1
  7. #define FALSE 0
  8. #define MAX_LENGTH_OF_INPUT_WORD 20
  9. #define MAX_ARRAY_SIZE 100000
  10.  
  11. int main (int argc, char *argv[]) {
  12.    char word[MAX_LENGTH_OF_INPUT_WORD];
  13.    char palindromeStore[MAX_ARRAY_SIZE];
  14.    int scanCount =  0;
  15.    while (fgets(word, sizeof(word),stdin)!= NULL) {
  16.  
  17.       word[strlen(word) - 1] = '\0';
  18.  
  19.       // suppose isPalindrome is true
  20.       // note: 'i' is a counter that
  21.       // will be used later
  22.       int isPalindrome = TRUE;
  23.       int i = 0;
  24.  
  25.       // checks if strlen(word) is odd or even
  26.       // by testing if strlen(word)%2 is 0
  27.       if (strlen(word)%2 == 0){
  28.          for(i = 0; i <= (strlen(word))/2; i++){
  29.             if (word[i] != word[strlen(word) - (i+1)]) {
  30.             // contradiction, hence isPalindrome
  31.             // is false
  32.                isPalindrome = FALSE;
  33.             }
  34.           }
  35.       } else {
  36.          for (i = 0; i <= (strlen(word)-1)/2; i++){
  37.             if (word[i] != word[strlen(word) - (i+1)]){
  38.                isPalindrome = FALSE;
  39.             }
  40.          }
  41.       }
  42.          /// palindromeStore
  43.          // this generates a store of words which we can
  44.          // later print for words that are palindroms
  45.          if (isPalindrome == TRUE){
  46.             printf("'%s' is a palindrome!\n", word);
  47.             word[strlen(word)] = '\n';
  48.             strcat(palindromeStore, word);
  49.             // this attaches word to palindromeStore, except
  50.             // the last character before the null byte is a
  51.             // \n so that it'll print out the string of
  52.             // palindromeStore with a new line for each word
  53.          } else {
  54.             printf("'%s' is not a palindrome.\n", word);
  55.          }
  56.          scanCount++;
  57.          printf("This is word: %d\n", scanCount);
  58.       }
  59.    printf("%s", palindromeStore);
  60.    return EXIT_SUCCESS;
  61. }