tic tac toe in c, trying to determine who wins - c

Trying to determine who wins in a game of tic tac toe,I am new to programming. Currently getting user has won after only entering 1 X or O input. Inputs must be entered with 2 int, row and coloumn. Any help is greatly appreciated!
#include <stdio.h>
#include <stdlib.h>
void drawBoard(char board[][3])
{
int rows, columns;
for ( rows = 0 ; rows < 3 ; rows++ )
{
for ( columns = 0 ; columns < 3 ; columns++ )
{
if(board[rows][columns]){
printf( "|%c", board[rows][columns] );
}else{
printf("| ");
}
}
printf("|\n");
}
}
int main()
{
char game[3][3]={{0}};
int totalEntry =0,row,column;
char value;
while(totalEntry<=9){
printf("Please choose x or o: ");
scanf("%c",&value);
getchar();
printf("Enter row number: ");
scanf("%d",&row);
getchar();
printf("Enter Column number: ");
scanf("%d",&column);
getchar();
game[row][column] = value;
drawBoard(game);
if((game[0][0] == game[0][1]) && (game[0][1] == game[0][2]) && game[0][0] != 'x')
if((game[1][0] == game[1][1]) && (game[1][1] == game[1][2]) && game[1][0] != 'x')
if((game[2][0] == game[2][1]) && (game[2][1] == game[2][2]) && game[2][0] != 'x')
if((game[0][0] == game[1][0]) && (game[1][0] == game[2][0]) && game[0][0] != 'x')
if((game[0][1] == game[1][1]) && (game[1][1] == game[2][1]) && game[0][1] != 'x')
if((game[0][2] == game[1][2]) && (game[1][2] == game[2][2]) && game[0][2] != 'x')
if((game[0][0] == game[1][1]) && (game[1][1] == game[2][2]) && game[0][0] != 'x')
if((game[2][0] == game[1][1]) && (game[1][1] == game[0][2]) && game[2][0] != 'x')
printf("User x has won!");
if((game[0][0] == game[0][1]) && (game[0][1] == game[0][2]) && game[0][0] != 'o')
if((game[1][0] == game[1][1]) && (game[1][1] == game[1][2]) && game[1][0] != 'o')
if((game[2][0] == game[2][1]) && (game[2][1] == game[2][2]) && game[2][0] != 'o')
if((game[0][0] == game[1][0]) && (game[1][0] == game[2][0]) && game[0][0] != 'o')
if((game[0][1] == game[1][1]) && (game[1][1] == game[2][1]) && game[0][1] != 'o')
if((game[0][2] == game[1][2]) && (game[1][2] == game[2][2]) && game[0][2] != 'o')
if((game[0][0] == game[1][1]) && (game[1][1] == game[2][2]) && game[0][0] != 'o')
if((game[2][0] == game[1][1]) && (game[1][1] == game[0][2]) && game[2][0] != 'o');
printf("User o has won!");
break;
}
return 0;
}

I would have used something like following, using a loop. Note that since the board is initialized to zero, assigning winner to a board position which still has zero means that a winner isn't found even if the test for all equal passes.
//declare this outside the main while-loop:
char winner=0;
for(i=0;i<3 && !winner;i++) {
if(game[i][0]==game[i][1] && game[i][1]==game[i][2])
winner=game[i][0]; // across
else if(game[0][i]==game[1][i] && game[1][i]==game[2][i])
winner=game[0][i]; // down
}
if( !winner && ( game[0][0]==game[1][1] && game[1][1]==game[2][2] ||
game[0][2]==game[1][1] && game[1][1]==game[2][0] ) )
winner = game[1][1]; // diagonal
if(winner) {
printf( "Winner is %c!\n",winner );
break;
}

IckIckIck.
First, it'll make you life much easier (at least for determining the winner), if you use a one-dimenisional array of 9 elements.
Then, write a function which test one possible win and returns true or false.
bool Test(char board[9], char player, int a, int b, int c)
{
return board[a] == player
&& board[b] == player
&& board[c] == player;
}
Then Test(game, 'x', 0,3,6); -- tests of x wins down left side.

The logic in your conditional is incorrect. Imagine the board looks like this. 0 denotes an empty spot.
0 0 0
0 0 x
0 0 0
The statement if((game[0][0] == game[0][1]) && (game[0][1] == game[0][2]) && game[0][0] != 'x') will evaluate to true because game[0][0] is empty game[0][1] is empty and game[0][2] is empty so they are all equal.
Also since game[0][0] is empty, it is not equal to 'x'.
So changing your != to == will help in that regard.

Related

Keep getting warning "Suggest parentheses around '&&' within '||' for C program [duplicate]

This question already has answers here:
Why does Clang warn: `'&&' within '||'`?
(2 answers)
Closed 12 months ago.
printf(" 1| %d | %d | %d | %d | %d\n",
((coffee_strength == 'm' || coffee_strength == 'M') &&
(coffee1_type == 'l' || coffee1_type == 'L') ||
(coffee_strength == 'r' || coffee_strength == 'R') &&
(coffee1_type == 'b' || coffee1_type == 'B')),
((coffee_maker == 'r' || coffee_maker == 'R') &&
(coffee1_grindSize == 'c' || coffee1_grindSize == 'C') ||
(coffee_maker == 'C' || coffee_maker == 'c') &&
(coffee1_grindSize == 'f' || coffee1_grindSize == 'F')),
((coffee_servings >= 1) && (coffee_servings <= 4) &&
(coffee1_weight >= 0) && (coffee1_weight <= 250) ||
((coffee_servings >= 1) && (coffee_servings <= 9) &&
(coffee1_weight == 500)) ||
((coffee_servings >= 10) && (coffee1_weight == 1000))),
(((coffee_cream == 'y' || coffee_cream == 'Y') &&
(coffee1_cream == 'y' || coffee1_cream == 'Y') ||
(coffee_cream == 'n' || coffee_cream == 'N') &&
(coffee1_cream == 'n' || coffee1_cream == 'N'))),
((coffee1_temp >= 60.0) && (coffee1_temp <= 69.9) &&
(coffee_maker == 'r' || coffee_maker == 'R') ||
((coffee1_temp >= 70.0) && (coffee_maker == 'c' || coffee_maker == 'C'))));
This is my code that I am trying to compile. Everytime I run it I still get the issue related to bracket placement. I have edited my code many times to try and solve this but the issue still persists. Does anyone have any suggestions as to what I should do?
Operator && has higher precedence than operator ||. Therefore, though legal, the compiler is telling you code that does this:
a && b || c && d
May not be doing what you intended. The compiler will treat that as
(a && b) || (c && d)
but for all it knows, you intended any one of a number of other things.
Perhaps this:
a && (b || (c && d))
or... this:
((a && b) || c) && d
Or maybe... this:
a && (b || c) && d
etc.
It makes a difference, and mistakes like this are common, so although your code will compile, it will do so with warnings when appropriately asked to do so (and it's always appropriate to ask). Not because it is wrong, but rather because you didn't make intent clear enough that the warning monkeys were subdued (which in this case, and most cases, is a good thing) To ensure you get what you expected, the compiler is asking you to clarify your expression via parenthesis. The -Wlogical-op-parentheses, included with -Wall, is the likely candidate telling you this if you're using gcc or clang.
By the looks of it, you've taken up the mantle of trying to address this, but you missed several instances. For example:
((coffee_servings >= 1) && (coffee_servings <= 4) &&
(coffee1_weight >= 0) && (coffee1_weight <= 250) || // <== here
((coffee_servings >= 1) && (coffee_servings <= 9) &&
(coffee1_weight == 500)) ||
((coffee_servings >= 10) && (coffee1_weight == 1000)))
There are others below, which I leave for you to find (they're pretty obvious once you start nesting your parens and realize just how many places a && b || c pops up).
(((coffee_cream == 'y' || coffee_cream == 'Y') &&
(coffee1_cream == 'y' || coffee1_cream == 'Y') ||
(coffee_cream == 'n' || coffee_cream == 'N') &&
(coffee1_cream == 'n' || coffee1_cream == 'N'))),
and here:
((coffee1_temp >= 60.0) && (coffee1_temp <= 69.9) &&
(coffee_maker == 'r' || coffee_maker == 'R') || // <== here
((coffee1_temp >= 70.0) && (coffee_maker == 'c' || coffee_maker == 'C')))
Sorry to say, but that seems like making out everything by looking at space. There are millions of stars, planets, asteroids, comets, meteors.
But I extremely appreciate your effort on formatting. Not a joke.
The problem seems to be here:
&& (coffee1_temp <= 69.9) &&
(coffee_maker == 'r' || coffee_maker == 'R') ||
((coffee1_temp >= 70.0) && (coffee_maker == 'c' || coffee_maker == 'C'))));
Let's expand it:
&& (coffee1_temp <= 69.9) && (coffee_maker == 'r' || coffee_maker == 'R') || ((coffee1_temp >= 70.0) && (coffee_maker == 'c' || coffee_maker == 'C'))));
I am not sure what you want to acheive in that line, but here is a example that might fix the error in that line:
&& (coffee1_temp <= 69.9) && ( (coffee_maker == 'r' || coffee_maker == 'R') || ((coffee1_temp >= 70.0) && (coffee_maker == 'c' || coffee_maker == 'C')) ) ));
Why does the error happen?
You have a a || c && d/a && b || c && d. The compiler warns you since you did not tell it what to do in that situation.
Example:
a && c || c && d,
What should happen first? c || c or a && c or c || c && d, etc... Extremely confusing...
Edit:
It also seems like you have this on many other places too.
You could just use separate if statements to make this clearer...after all it seems like it takes more time to read this than to write a new set of if statements.
Hopefully more readable:
(
(coffee_strength == 'm' || coffee_strength == 'M')
&& (coffee1_type == 'l' || coffee1_type == 'L')
|| (coffee_strength == 'r' || coffee_strength == 'R')
&& (coffee1_type == 'b' || coffee1_type == 'B')
),
(
(coffee_maker == 'r' || coffee_maker == 'R')
&& (coffee1_grindSize == 'c' || coffee1_grindSize == 'C')
|| (coffee_maker == 'C' || coffee_maker == 'c')
&& (coffee1_grindSize == 'f' || coffee1_grindSize == 'F')
),
(
(coffee_servings >= 1)
&& (coffee_servings <= 4)
&& (coffee1_weight >= 0)
&& (coffee1_weight <= 250)
|| (
(coffee_servings >= 1)
&& (coffee_servings <= 9)
&& (coffee1_weight == 500)
)
|| (
(coffee_servings >= 10)
&& (coffee1_weight == 1000)
)
),
(
(
(coffee_cream == 'y' || coffee_cream == 'Y')
&& (coffee1_cream == 'y' || coffee1_cream == 'Y')
|| (coffee_cream == 'n' || coffee_cream == 'N')
&& (coffee1_cream == 'n' || coffee1_cream == 'N')
)
),
(
(coffee1_temp >= 60.0)
&& (coffee1_temp <= 69.9)
&& (coffee_maker == 'r' || coffee_maker == 'R')
|| (
(coffee1_temp >= 70.0)
&& (coffee_maker == 'c' || coffee_maker == 'C')
)
)
Now, is it really hard to make out what the issues are?
Fix, THAT MIGHT NOT BEHAVE AS YOU INTENDED:
(
(
(coffee_strength == 'm' || coffee_strength == 'M')
&& (coffee1_type == 'l' || coffee1_type == 'L')
)
|| (
(coffee_strength == 'r' || coffee_strength == 'R')
&& (coffee1_type == 'b' || coffee1_type == 'B')
)
)
(
(
(coffee_maker == 'r' || coffee_maker == 'R')
&& (coffee1_grindSize == 'c' || coffee1_grindSize == 'C')
)
|| (
(coffee_maker == 'C' || coffee_maker == 'c')
&& (coffee1_grindSize == 'f' || coffee1_grindSize == 'F')
)
)
(
(
(coffee_servings >= 1)
&& (coffee_servings <= 4)
&& (coffee1_weight >= 0)
&& (coffee1_weight <= 250)
)
|| (
(coffee_servings >= 1)
&& (coffee_servings <= 9)
&& (coffee1_weight == 500)
)
|| (
(coffee_servings >= 10)
&& (coffee1_weight == 1000)
)
)
(
(
(
(coffee_cream == 'y' || coffee_cream == 'Y')
&& (coffee1_cream == 'y' || coffee1_cream == 'Y')
)
|| (
(coffee_cream == 'n' || coffee_cream == 'N')
&& (coffee1_cream == 'n' || coffee1_cream == 'N')
)
)
)
(
(
(coffee1_temp >= 60.0)
&& (coffee1_temp <= 69.9)
&& (coffee_maker == 'r' || coffee_maker == 'R')
)
|| (
(coffee1_temp >= 70.0)
&& (coffee_maker == 'c' || coffee_maker == 'C')
)
)
And as #user3386109 said, you can simplify statements checking for uppercase and lowercase like a == 'A' || a == 'a' to toupper(a) == 'A' using toupper(int c) and tolower(int c) from <ctype.h>

Is my negation of this if statement correct?

I am currently working with some legacy code that has this if statement:
if(c == 30 || c == 3 || c == 4 || r == 'Y' || t == 'X' ||( s>= 50000 && s <= 50999))
{
//do nothing
}
else
{
function1();
}
I am trying to eliminate the unnecessary "do nothing" by negating the if, but I'm not sure what to do about the 's' part:
if((c != 30 && c != 3 && c != 4 && r != 'Y' && t != 'X') ...what goes here?...)
{
function1();
}
I've tried both && (s >= 50000 || s <= 50999) and ||(s < 50000 && s > 50999) but those failed the test cases I ran. When I tried &&( s < 50000 || s > 50999)) all my test cases succeeded, but I know I can't test for everything and so I'm hoping someone with a stronger grasp of logical negation can tell me with certainty whether this is correct.
If you have a logical expression like
expr1 || expr2
then its negation
!( expr1 || expr2 )
is equivalent tp
!expr1 && !expr2
And vice versa if you have an expression like this
expr1 && expr2
then its negation
!( expr1 && expr2 )
is equivalent to
!expr1 || !expr2
So to negate this expression
c == 30 || c == 3 || c == 4 || r == 'Y' || t == 'X' ||( s>= 50000 && s <= 50999)
like
!( c == 30 || c == 3 || c == 4 || r == 'Y' || t == 'X' ||( s>= 50000 && s <= 50999))
we will get
!( c == 30 )&& !( c == 3 ) && !( c == 4 ) && !( r == 'Y' ) && !(t == 'X' ) && !( s>= 50000 && s <= 50999)
then
c != 30 && c != 3 && c != 4 && r != 'Y'&& t != 'X' && ( !( s>= 50000 ) || !( s <= 50999))
and at last
c != 30 && c != 3 && c != 4 && r != 'Y'&& t != 'X' && ( s < 50000 || s > 50999 )
This will work. However, it's a little redundant:
if((c != 30 && c != 3 && c != 4 && r != 'Y' && t != 'X') && (s < 50000 || s > 50999))
An easier way would be to just negate the entire statement:
if(!(c == 30 || c == 3 || c == 4 || r == 'Y' || t == 'X' ||( s>= 50000 && s <= 50999)))

How can I predict the winning position for player X or O in a tic tac toe game and tell the user the position?

so I wrote a function that allows the user to find if any next move or next play for any player X or O who has the turn to play is winning move. The function will print the cell number or ID that if the player places an X or O in it he/she will win the game. If there is more than one cell where the player could place his symbol in and win the game the function should print all the winning cells for that player.
I made a simple implementation of the code but it seems to work but not print right position. Any help would be great. Thanks.
void ListWinningCells(int m, int n, char board[][n])
{
int check = IsValidBoard(m, n, board);
if(check == 1){
// for row:
if((board[0][0] == 'X' || board[0][0] == 'O') && (board[0][2] == 'X' || board[0][2] == 'O')){
printf("Winning cell is 2 for player %c\n", board[0][0]);
}
if((board[1][0] == 'X' || board[1][0] == 'O') && (board[1][2] == 'X' || board[1][2] == 'O')){
printf("Winning cell is 5 for player %c\n", board[0][0]);
}
if((board[2][0] == 'X' || board[2][0] == 'O') && (board[2][2] == 'X' || board[2][2] == 'O')){
printf("Winning cell is 8 for player %c\n", board[0][0]);
}
// for column
if((board[0][0] == 'X' || board[0][0] == 'O') && (board[2][0] == 'X' || board[2][0] == 'O')){
printf("Winning cell is 4 for player %c\n", board[0][0]);
}
if((board[0][1] == 'X' || board[0][1] == 'O') && (board[2][1] == 'X' || board[2][1] == 'O')){
printf("Winning cell is 5 for player %c\n", board[0][0]);
}
if((board[0][2] == 'X' || board[0][2] == 'O') && (board[2][2] == 'X' || board[2][2] == 'O')){
printf("Winning cell is 6 for player %c\n", board[0][0]);
}
// for diagonal
if((board[0][0] == 'X' || board[0][0] == 'O') && (board[2][2] == 'X' || board[2][2] == 'O')){
printf("Winning cell is 5 for player %c\n", board[0][0]);
}
if((board[0][2] == 'X' || board[0][2] == 'O') && (board[2][0] == 'X' || board[2][0] == 'O')){
printf("Winning cell is 5 for player %c\n", board[0][0]);
}
}
else{
IsValidBoard(m, n, board);
}
}
Lets take a look at your first if statement:
if((board[0][0] == 'X' || board[0][0] == 'O') && (board[0][2] == 'X' || board[0][2] == 'O'))
If 'X' is on board[0][0] and 'O' is on board[0][2] the statement would be true, but that isn't correct. It should be:
if((board[0][0] == 'X' || board[0][0] == 'O') && (board[0][2] == board[0][0]))
So it would return that the winning cell is 2. You should also consider that some cases are not being taken into account, as cell 1 and cell 3 being the winning cell, I do not know if it is the intention.
You should also consider using for loops to check the positions.

c how to check the first char of an array

I am trying to take in 10 characters over a serial console and add them to an array called buffer. The first character needs to be 'L' or 'S' and the next characters either '1' or '0'.
The code passes the first if statement ok. But the line if((buffer[0] != 'L') || (buffer[0] != 'S')) doesn't seem to work even when I enter 'L' OR 'S'.
Is there anything wrong with using the buffer[0] != notation?
int main(void)
{
char ch;
char buffer[10] = "";
putstring("Enter 9 characters beginning with 'L' or 'S' and 8 digits\r\n");
for (int i = 0; i < 9; i++) {
ch = getcharacter();
if ((ch == '0') || (ch == '1') || (ch == 'L') || (ch == 'S')) {
buffer[i] = ch;
//check first character
if((buffer[0] != 'L') || (buffer[0] != 'S')) {
printf("First letter must be L or S\r\n");
goto error;
}
error:
return -1;
}
}
}
int getcharacter(void) {
char c = 0;
const uint32_t recieve_ready = 1 << 7;
//disable interrupt for a read ready
*uart_control_reg = 0;
while (1) {
//check if RRDY bit is set
if ((*uart_status_reg) & recieve_ready) {
c = *uart_rxdata_reg;
break;
}
}
return ((char) c);
}
if((buffer[0] != 'L') || (buffer[0] != 'S'))
is wrong, you need
if((buffer[0] != 'L') && (buffer[0] != 'S'))
or
if (!(buffer[0] == 'L' || buffer[0] == 'S'))
Your original code was "if the char is not L or the char is not S" which is always true. If the char is L, then the second part was true, making the whole if statement true.
Just noticed Chris Turner's comment above. The return -1 is always executed, move it to replace the line that says goto error.
Try using
if((buffer[0] != 'L') && (buffer[0] != 'S'))
instead of
if((buffer[0] != 'L') || (buffer[0] != 'S'))
Just some logic problem here. According to your code, the char needs to be equal to 'L' AND 'S' to avoid the condition, which is never the case !

Is there something wrong with my gets function? we haven't been though about fgets yet in class. only gets

for example when I type in a string "Hello", when I press 'a' it should print "There are 2 vowels." Instead, it says there are 0. I'm new to programming and this is the first language I am learning. Help?
/*
Student: Josiah Eleazar T. Regencia
Course: BSIT 1
Subject: SCS 101
Professor: Daniel B. Garcia
Problem definition:
Write a menu program that will count the vowels and consonants in the string
*/
#include <stdio.h> //printf and scanf functions
#include <string.h> //string functions
#include <stdlib.h>
#include <conio.h>
#define PROMPT "Type in a word, a phrase, or a sentence." //instucts the user
/*
Declaring the function for the user's menu
*/
void menu();
/*
Declaration of function needed to count the vowels in the string
*/
int vowel_count(char *stringInput); //declaring the function to count the vowels sounds
/*
Declaration of function needed to count the consonants in the stringn
*/
int consonant_count(char *stringInput); //declaring the functions to count the consonant sounds
/*
Declaring the function needed to convert the streeting to uppercase
*/
int uppercase(char *stringInput);
/*
Declaring the function needed to convert the streeting to uppercase
*/
int lowercase(char *stringInput);
int main () {
char userInput[100]; // the string the user inputs
char commandKey[1]; //this key is for the menu
char newInput[100]; //this is for the new input to user will put in
int stringLength; //to identify the length of the string
/*
Variables for counting the vowels and consonants
*/
int consonantCount;
printf("%s\n\n", PROMPT); //instucts the user
gets(userInput);
stringLength = strlen(userInput); //gets the length of the string
//fgets(userInput, 100, stdin);
/*if(stringLength > 0 && userInput[stringLength - 1] == '\n') {
userInput[stringLength - 1] ='\0';
}*/
menu(); //prints out the menu for the user to pick his options
/*
The loop will run what the user asks the program to run while at the same time also asking
what the programmer wants next
*/
while(*commandKey != 'X' || *commandKey != 'x') {
//int commandLength; //length of the command key
printf("Enter your menu selection: ");
gets(commandKey);
/*commandLength = strlen(commandKey);
fgets(commandKey, 100, stdin);
if(commandLength > 0 && commandKey[commandLength - 1] == '\n') {
commandKey[commandLength - 1] ='\0';
}*/
if(*commandKey == 'A' || *commandKey == 'a') {
int vowelCount;
vowelCount = vowel_count(userInput);
printf("There are %d vowels.\n\n", vowelCount);
}
if(*commandKey == 'B' || *commandKey == 'b') {
consonantCount = consonant_count(userInput);
printf("There are %d consonants.\n\n", consonantCount);
}
if(*commandKey == 'C' || *commandKey == 'c') {
/*
This condition simply converts the input string to all lowercase letters
*/
lowercase(userInput);
}
if(*commandKey == 'D' || *commandKey == 'd') {
/*
This condition simply converts the input string to all lowercase letters
*/
uppercase(userInput);
}
if(*commandKey == 'E' || *commandKey == 'e') {
/*
Prints the current string
if the string was converted in lowercase letters, the outcome would be all lowercase letters
if the string was converted in uppercase letters, the outcome would be all uppercase letters
*/
printf("%s\n\n", userInput);
}
if(*commandKey == 'F' || *commandKey == 'f') {
/*
When the user wants to test a new string, this is the condition for the user
to automatically ask of it
*/
printf("%s\n", PROMPT);
gets(newInput);
strcpy(userInput, newInput);
}
if(*commandKey == 'M' || *commandKey =='m') {
/*
In case the user forgets, this will serve as a reminder of the menu
*/
menu();
}
if(*commandKey == 'X' || *commandKey == 'x') {
printf("Goodbye!\n");
break;
}
}
}
//Function that displays the menu.
void menu() {
/*
These are the set of command keys given to the user
*/
printf("\n\n\n");
printf("PRESS:\n\n");
printf(" A - Count the number of vowels in the string.\n");
printf(" B - Count the number of consonants in the string.\n");
printf(" C - Convert the string to uppercase.\n");
printf(" D - Convert the string to lowecase.\n");
printf(" E - Display the current string.\n");
printf(" F - Enter another string.\n");
printf("\n");
printf(" M - Display this menu.\n");
printf(" X - Exit the program.\n");
printf("\n\n");
}
/*
Defining the function for the vowel counting
*/
int vowel_count(char *stringInput) {
if ( *stringInput == '\0')
return 0;
else
return vowel_count(stringInput + 1) + (*stringInput == 'a' || *stringInput == 'A')
+ (*stringInput == 'e' || *stringInput == 'E')
+ (*stringInput == 'i' || *stringInput == 'I')
+ (*stringInput == 'o' || *stringInput == 'O')
+ (*stringInput == 'u' || *stringInput == 'U');
}
/*
Defining the function for the vowel counting
*/
int consonant_count(char *stringInput) {
if (*stringInput == '\0')
return 0;
else
return consonant_count(stringInput + 1) + (*stringInput == 'b' || *stringInput == 'B')
+ (*stringInput == 'c' || *stringInput == 'C')
+ (*stringInput == 'd' || *stringInput == 'D')
+ (*stringInput == 'f' || *stringInput == 'F')
+ (*stringInput == 'g' || *stringInput == 'G')
+ (*stringInput == 'h' || *stringInput == 'H')
+ (*stringInput == 'j' || *stringInput == 'J')
+ (*stringInput == 'k' || *stringInput == 'K')
+ (*stringInput == 'l' || *stringInput == 'L')
+ (*stringInput == 'm' || *stringInput == 'M')
+ (*stringInput == 'n' || *stringInput == 'N')
+ (*stringInput == 'p' || *stringInput == 'P')
+ (*stringInput == 'q' || *stringInput == 'Q')
+ (*stringInput == 'r' || *stringInput == 'R')
+ (*stringInput == 's' || *stringInput == 'S')
+ (*stringInput == 't' || *stringInput == 'T')
+ (*stringInput == 'v' || *stringInput == 'V')
+ (*stringInput == 'w' || *stringInput == 'W')
+ (*stringInput == 'x' || *stringInput == 'X')
+ (*stringInput == 'y' || *stringInput == 'Y')
+ (*stringInput == 'z' || *stringInput == 'Z');
}
/*
Defining the function for the conversion of the string to all uppercase letters
*/
int uppercase(char *stringInput) {
while(*stringInput) {
*stringInput = toupper(*stringInput);
stringInput++;
}
}
/*
Defining the function for the conversion of the string to all uppercase letters
*/
int lowercase(char *stringInput) {
while(*stringInput) {
*stringInput = tolower(*stringInput);
stringInput++;
}
}
This loop will never stop:
while(*commandKey != 'X' || *commandKey != 'x')
A loop stops when its condition is false. For *commandKey != 'X' || *commandKey != 'x' to be false, it is logically equivalent for *commandKey == 'X' && *commandKey == 'x' to be true, which doesn't quite work. *commandKey can't be both 'X' and 'x'.
Instead, you want:
while(*commandKey != 'X' && *commandKey != 'x')
Which will stop when *commandKey is 'X' or 'x'.
Also, you never initialized commandKey, so you can't test it in the loop (using values of uninitialized variables is undefined behavior). And if you want to treat it as a string, you need to declare it such that it holds at least 2 characters, because the null character is needed:
char commandKey[2] = { 0 };
This will initialize commandKey to an empty string. Remember that gets() will null-terminate the string, so, even if your command is just one key, gets() will need at least 2 positions to fill - the character command and the null terminating byte. Alternatively, you can leave commandKey as is (provided that you initialize it), and use getchar() instead, which reads one single character.

Resources