C Programming: Palindrome

CS 3410 Spring 2018


Due: Tuesday, Feb 27th, 2018 at 11:59 PM. Submit your palindrome.c on CMS.


Overview

For this assignment, you will write a C program that inputs a string from the command line and outputs whether or not the string is a palindrome. A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. You can assume that the input does not contain any white spaces. Submit the C file on CMS. Your program must compile and run on the course provided VM and /or the CSUG computers.

NOTE: On all problem sets in CS3410, submit code which adheres to the C99 standard (for gcc, compile with -std=c99).

To compile the C program you should use the following command, where palindrome.c is the C source file and testpalindrome is the executable file.

$ gcc -Wall -std=c99  palindrome.c  -o testpalindrome

You can run the program with the command:

$ ./testpalindrome
You should now see the fruits of your labor!

For example:

Input a String: racecar
racecar is a palindrome

Input a String: Racecar
Racecar is not a palindrome

Input a String: a
a is a palindrome

Input a String: whales
whales is not a palindrome

You may assume the input string is less than 100 characters long. You may not use any functions from the string.h library. stdio.h is the only library you can use.

A skeleton has been provided. Do NOT change any of the print statements. You can change the variable names.

#include <stdio.h>

int main()
{
    char str[100];
    int i, length=0, flag=0, start, end; 
  
    printf("Input a string: ");

  
    // Read in input from the command line

    // Find the length of the string.
    // Hint: How do you know when a string ends in C?

    // Check if str is a palindrome.


    if(flag==1)  
        printf("%s is not a palindrome.\n", str);
    else
        printf("%s is a palindrome.\n", str);

    return 0;
}

If you have a problem or question, you can have a look at Professor Anne Bracy's book: All of Programming. Many common questions are also the top result on your favorite search engine.