so if my string is NULL or empty I want the following errors to print. It's not working. What am I doing wrong.
Thank you!
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void getString( char* );
void printString (const char *);
void reverseString (const char *str, char *reverse);
int main(void)
{
char command[MAX_CMD_LEN];char string[SIZE];
char newstr[SIZE];
char reverse[SIZE];
do {
printf("cmd> ");
if(fgets(command, MAX_CMD_LEN, stdin) != NULL){
if((p = strchr( command, '\n')) != NULL)
*p = '\0';
}
if(strcmp(command ,"new")==0) {
getString(string);
}
if(strcmp(command, "list")==0) {
printString(string);
}
if(strcmp(command, "rev")==0) {
reverseString(string, reverse);
}
else { //if any command entered besides these 3
printf("Not valid command"); //ERROR CHECK #1
}
}
while (flag==1);
return 0;
}
void getString(char *string)
{
printf("Please enter your string");
fgets(string, SIZE, stdin);
}
void printString (const char *string)
{
if(string == NULL){
printf("No string entered"); //ERROR CHECK #2
}
else{
printf( "\n\nHere is the text you entered:\n%s\n", string);
}
}
void reverseString(const char *string, char *reverse)
{
int len =0, i, j =0;
if(string == NULL){
printf("Null string"); //ERROR CHECK #3
}
while(string[len])
len++;
for(i=len-1;i>=0;i--)
{
reverse[j]=string[i];
j++;
}
reverse[j] = '\0';
printf("%s", reverse);
}