Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "pal.h"
/* Display greeting message */
/* name: PrintGreeting */
/* inputs: none */
/* returned value: none */
void PrintGreeting ()
{
printf( "\nWelcome to the Strings program.");
printf( "\nThis program will allow you to anaylze any of the strings");
printf( "\nfrom the input file in several different ways.");
printf( "\n\nAlso: a palindrome is defined as any word or phrase that is");
printf( "\nread the same way backwards and forwards. For example, the word");
printf( "\nracecar is a palindrome.");
}
/* Display instructions */
/* name: PrintInstructions */
/* inputs: none */
/* returned value: none */
void PrintInstructions ()
{
printf( "\n\nTo use this program, simply select one of the strings you");
printf( "\nwant to analyze, or 0 to quit. After selecting a string");
printf( "\na menu will appear which will enable you to test that string");
printf( "\nin a number of different ways. Select the corresponding");
printf( "\nletter, or Q to quit.\n");
}
/* Reads in number of strings */
/* name: ReadNumStrings */
/* inputs: File pointer */
/* returned values: number of strings */
int ReadNumStrings ( FILE *data )
{
int numStrings = 0;
char buffer[101];
while ( fgets ( buffer, 101, data ) != NULL )
{
numStrings++;
}
return numStrings;
}
/* Fills the array with the strings from the data file */
/* name: FillArray */
/* inputs: File pointer, array of strings */
/* returned values: none */
void FillArray ( FILE *data, char **strArray, int numStrings )
{
char buf[100];
int counter = 0;
int strLen;
int i;
rewind ( data );
while ( fgets ( buf, 100, data ) != NULL )
{
strLen = strlen ( buf );
strArray[counter] = (char *) malloc (strLen * sizeof (char));
strcpy ( strArray[counter], buf );
printf("%2d = %s\n", counter, strArray[counter]);
counter++;
}
for ( i = 0; i < numStrings; i++ )
{
printf("strArray[%2d] = %s\n", i, strArray[i]);
}
printf("\nBeginning MenuOne function\n");
MenuOne ( numStrings, strArray );
}
/* Controls the first level menu */
/* name: MenuOne */
/* inputs: Number of strings, array of strings */
/* returned values: none */
void MenuOne ( int numStrings, char **strArray )
{
int i, userInput = -41;
printf("\nMenuOne function opened. %d\n", numStrings );
for ( i = 0; i < numStrings; i++ )
{
printf("strArray[%2d] - %s\n", i, strArray[i] );
}
printf ("Please select which string you'd like to analyze, or 0 to quit.\n");
scanf ("%d", &userInput );
printf("%d is the user input\n", userInput);
while( userInput < 0 || userInput > numStrings )
{
fprintf(stderr, "\nPlease enter a valid response. " );
scanf("%d", &userInput );
}
MenuTwo ( userInput, strArray );
}
/* Controls the second level menu */
/* Name: MenuTwo */
/* inputs: user integer selection, array of strings */
/* returned values: true/false ending condition */
int MenuTwo ( int input, char **strArray )
{
char select = 'Q';
int end, numVowels, numCons, length, numWords, truefalse = 0;
printf("\nMenuTwo just opened.\n");
for ( end = 0; end < 19999; end++ )
{
printf(" \n Analysis of the string :");
printf(" \n %s\n", strArray[input - 1] );
printf(" \n P - Is this string a palindrome ?");
printf(" \n V - How many vowels ?");
printf(" \n C - How many consonants ?");
printf(" \n L - What's its length ?");
printf(" \n W - How many words ?");
printf("\n\n");
printf(" \n Q - Quit ");
printf("\nEnter choice : ");
scanf("%c", &select );
if( select == 'Q' || select == 'q' )
{
end = 20000;
}
else if( select == 'P' || select == 'p' )
{
truefalse = IsPalindrome( input, strArray );
if ( truefalse == 1 )
{
printf( "\nThe following string is a palindrome : %s\n", strArray[input-1]);
}
else
{
printf(" \nThe following string is not a palindrome : %s\n", strArray[input-1]);
}
}
else if( select == 'V' || select == 'v' )
{
numVowels = NumVowels ( input, strArray );
printf( "\nThere are %d vowels in : %s ", numVowels, strArray[input-1]);
}
else if( select == 'C' || select == 'c' )
{
numCons = NumCons ( input, strArray );
printf( "\nThere are %d consonants in : %s ", numCons, strArray[input-1]);
}
else if( select == 'L' || select == 'l' )
{
length = strlen ( strArray[input-1] );
printf("\n%d is the length of : %s", length, strArray[input-1] );
}
else if( select == 'W' || select == 'w' )
{
numWords = NumWords ( input, strArray );
printf("\nThere's %d word(s) in : %s ", numWords, strArray[input-1] );
}
}
return 1;
}
/* Tests to see if the string is a palindrome */
/* name: IsPalindrome */
/* inputs: user input, array of strings */
/* returned values: true/false statement */
int IsPalindrome ( int input, char **strArray )
{
int output;
return output;
}
/* Determines the number of vowels in the string */
/* name: NumVowels */
/* inputs: user input, array of strings */
/* returned values: number of vowels in string */
int NumVowels ( int input, char **strArray )
{
int numVowels;
return numVowels;
}
/* Determines the number of consonants in the string */
/* name: NumCons */
/* inputs: user input, array of strings */
/* returned values: number of consonants in the string */
int NumCons ( int input, char **strArray )
{
int numCons = 0;
int i;
if ( strArray[input-1][i] == 'n' )
{
numCons++;
}
return numCons;
}
/* Determines how many words are in the string */
/* name: NumWords */
/* inputs: user input, array of strings */
/* returned values: number of words in the string */
int NumWords ( int input, char **strArray )
{
int numWords;
return numWords;
}