I'm trying to construct a program that has the user input the date in this format '01'14'2013', and outputs it into this format 'january 14, 2013'. I am trying to copy the string that holds the input from the user onto a different string, to later concatenate it onto the original string without the first and second index of the strings, so that I only have '/14/2013', from the original string, and then replace the '/' with ' ' so that it reads the month, day and the year....but for some reason, when I try to copy the original string from input onto another string( the one I plan to concatenate later), it doesn't copy effectively, am i missing something..?
#include <stdio.h>
#include <string.h>
int main()
{
char date[100];
char month[100];
char array[12][100] ={"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
char month2[100];
printf(" Please enter a date ");
fgets( date, 100, stdin);
strcpy(month2, month);
if( date[0] == '0' && date[1] == '1')
{
strcpy(month, array[0]);
}
else if( date[0] =='0' && date[1] == '2')
{
strcpy(month, array[1]);
}
else if( date[0] =='0' && date[1] == '3')
{
strcpy(month, array[2]);
}
else if( date[0] =='0' && date[1] == '4')
{
strcpy(month, array[3]);
}
else if( date[0] =='0' && date[1] == '5')
{
strcpy(month, array[4]);
}
else if( date[0] == '0' && date[1] == '6')
{
strcpy(month, array[5]);
}
else if( date[0] =='0' && date[1] == '7')
{
strcpy(month, array[6]);
}
else if( date[0] =='0' && date[1] == '8')
{
strcpy(month, array[7]);
}
else if( date[0] =='0' && date[1] == '9')
{
strcpy(month, array[8]);
}
else if( date[0] =='1' && date[1] == '0')
{
strcpy(month, array[9]);
}
else if( date[0] =='1' && date[1] == '1')
{
strcpy(month, array[10]);
}
else if( date[0] =='1' && date[1] == '2')
{
strcpy(month, array[11]);
}
printf("%s \n", month);
printf("%s \n", month2);
return 0;
}
strcpy(month2, month);
Neither month nor month2 have been initialized to anything useful at this point. Their contents are indeterminate and calling strcpy with something other than a properly terminated C-string invokes undefined behavior.
Looks like a typo to me.
Lesser code, But without some validations.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char date[100];
char month[100];
char array[12][100] = {"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
printf(" Please enter a date ");
fgets( date, 100, stdin);
char month2[100];
strcpy(month2, date);
month2[2] = '\0';
strcpy(month, array[atoi(month2) - 1]);
printf("%s \n", month);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <ctype.h>
typedef struct mon {
const char *name;
const int len;
} Month;
#define M(x){ x " ", sizeof(x)}
Month month[] = { {"", 0}, //dummy
M("January"), M("Febuary"), M("March"), M("April"),
M("May"), M("June"), M("July"), M("August"),
M("September"), M("October"), M("November"), M("December")
};
int main(){
char in_date[128];
char out_date[128] = "";
int m = 0, pos;
printf(" Please enter a date E.g MM/DD/YYYY\n");
fgets( in_date, sizeof(in_date), stdin);
if(in_date[0] == '0'){
if(isdigit(in_date[1]) && in_date[1] != '0'){
m = in_date[1] - '0';
pos = month[m].len;
memcpy(out_date, month[m].name, pos);
}
} else if(in_date[0] == '1'){
if('0' <= in_date[1] && in_date[1] <= '2'){
m = 10 + in_date[1] - '0';
pos = month[m].len;
memcpy(out_date, month[m].name, pos);
}
}
if(m){
memcpy(out_date + pos, in_date + 3, 7);
out_date[pos + 2] = ',';
out_date[pos + 7] = '\0';
printf("%s\n", out_date);
} else {
printf("invalid month\n");
}
return 0;
}
Related
I was working on learning c code and was making a tic-tac-toe game. The Boolean issue was fixed. Now the issue is that it is looping the printf("There is no empty space!"); and prinf("Invalid !!!"); after it take the player1 name. I also wanted to know if the line where I printed the array with the grid is correct or not.
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
char space[3][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
};
int row;
int column;
char token = 'x';
bool tie = false;
char n1[256];
char n2[256];
void functionboard()
{
char space[3][3] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
};
printf(" | | \n");
printf(" ", space[0][0], "| ", space[0][1], "| ", space[0][2], " \n");
printf("______|________|_____\n");
printf(" | | \n");
printf(" ", space[1][0], " | ", space[1][1], " | ", space[1][2], " \n");
printf("______|________|_____\n");
printf(" | | \n");
printf(" ", space[2][0], " | ", space[2][1], " | ", space[2][2], " \n");
printf(" | | \n");
}
void functionOne()
{
int dight;
if (token == 'x')
{
printf(n1, "please enter");
scanf("&d", &dight);
}
if (token == '0')
{
printf(n2, "please enter");
scanf("&d", &dight);
}
if (dight == 1)
{
row = 0;
column = 0;
}
if (dight == 2)
{
row = 0;
column = 1;
}
if (dight == 3)
{
row = 0;
column = 2;
}
if (dight == 4)
{
row = 1;
column = 0;
}
if (dight == 5)
{
row = 1;
column = 1;
}
if (dight == 6)
{
row = 1;
column = 2;
}
if (dight == 7)
{
row = 2;
column = 0;
}
if (dight == 8)
{
row = 2;
column = 1;
}
if (dight == 9)
{
row = 2;
column = 2;
}
else if (dight < 1 || dight > 9)
{
prinf("Invalid !!!");
}
if (token == 'x' && space[row][column] != 'x' && space[row][column] != '0')
{
space[row][column] = 'x';
token = '0';
}
else if (token == '0' && space[row][column] != 'x' && space[row][column] != '0')
{
space[row][column] = '0';
token = 'x';
}
else
{
printf("There is no empty space!");
functionboard();
}
functionOne();
}
bool functionDraw()
{
for (int i = 0; i < 3; i++)
{
if (space[i][0] == space[i][1] && space[i][0] == space[i][2] || space[0][i] == space[1][i] && space[0][i] == space[2][i])
return true;
}
if (space[0][0] == space[1][1] && space[1][1] == space[2][2] || space[0][2] == space[1][1] && space[1][1] == space[2][0])
{
return true;
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (space[i][j] != 'x' && space[i][j] != '0')
{
return false;
}
}
}
tie = true;
return false;
}
int main()
{
printf("Enter the name of the first player : \n");
scanf("%c", n1);
printf("Enter the name of the second player : \n");
scanf("%c", n2);
printf("%c is player1 so he/she will play first \n", n1);
printf("%c is player2 so he/she will play first \n", n2);
while (!functionDraw())
{
functionboard();
functionOne();
functionDraw();
}
if (token == 'x' && tie == false)
{
printf("%c Wins!!\n", n2);
}
else if (token == '0' && tie == false)
{
printf("%c Wins!!\n", n1);
}
else
{
printf("its a draw!!");
}
}
If the error you're getting is about the type bool and not the variable itself, see this question: you need to also include <stdbool.h> to be able to use the bool type.
There's no built-in bool type in classic C. You can use it though by using #include <stdbool.h>. The other solution is replacing it with an int since it can be used like a bool
What I'm trying to do here is a tie toe game, but when my code enters the do - while part, it ends the process by itself. Since I could not solve this part, I did not have a chance to try whether there are other problems with the code, unfortunately, I would be glad if you could help with this issue.
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
char box_area[] = { '0','1','2','3','4','5','6','7','8','9' };
struct Player {
bool turn;
char mark;
int ID;
};
int main()
{
int return_result;
int player_number;
struct Player player1;
struct Player player2;
struct Player users[2];
users[0] = player1;
users[1] = player2;
(player1.mark = 'X') && (player2.mark = 'O');
(player1.turn = true) && (player2.turn = false);
(player1.ID = 1) && (player2.ID = 2);
do
{
box_creat();
for (int i = 0; i < 2; i++) {
if (users[i].turn == true)
{
make_move(users[i].ID);
box_creat();
users[i].turn = false;
users[i + 1].turn = true; \\ I made the logic of this section wrong
\\I will try to fix it, I realized after I sent the question
}
}
return_result = check_the_winner();
} while (return_result == 1 || return_result == -1);
return 0;
}
void box_creat(void) {
printf("| %c | %c | %c |", box_area[0], box_area[1], box_area[2]);
printf("\n\n");
printf("| %c | %c | %c |", box_area[3], box_area[4], box_area[5]);
printf("\n\n");
printf("| %c | %c | %c |", box_area[6], box_area[7], box_area[8]);
}
void make_move(int Player_ID)
{
int choice;
printf("Please select a area between 0-9 ");
scanf("%d", choice);
if (choice == '0' && box_area[0] == '0')
{
if (Player_ID == 1) {
box_area[0] = 'X';
}
else {
box_area[0] = 'O';
}
}
else if (choice == '1' && box_area[1] == '1')
{
if (Player_ID == 1) {
box_area[1] = 'X';
}
else {
box_area[1] = 'O';
}
}
else if (choice == '2' && box_area[2] == '2')
{
if (Player_ID == 1) {
box_area[2] = 'X';
}
else {
box_area[2] = 'O';
}
}
else if (choice == '3' && box_area[3] == '0')
{
if (Player_ID == 1) {
box_area[3] = 'X';
}
else {
box_area[3] = 'O';
}
}
else if (choice == '4' && box_area[4] == '0')
{
if (Player_ID == 1) {
box_area[4] = 'X';
}
else {
box_area[4] = 'O';
}
}
else if (choice == '5' && box_area[5] == '0')
{
if (Player_ID == 1) {
box_area[5] = 'X';
}
else {
box_area[5] = 'O';
}
}
else if (choice == '6' && box_area[6] == '0')
{
if (Player_ID == 1) {
box_area[6] = 'X';
}
else {
box_area[6] = 'O';
}
}
else if (choice == '7' && box_area[7] == '0')
{
if (Player_ID == 1) {
box_area[7] = 'X';
}
else {
box_area[7] = 'O';
}
}
else if (choice == '8' && box_area[8] == '0')
{
if (Player_ID == 1) {
box_area[8] = 'X';
}
else {
box_area[8] = 'O';
}
}
}
int check_the_winner(void)
{
if (box_area[0] && box_area[1] && box_area[2] == 'X' || 'O') {
return 1;
}
else if(box_area[3] && box_area[4] && box_area[5] == 'X' || 'O') {
return 1;
}
else if (box_area[6] && box_area[7] && box_area[8] == 'X' || 'O') {
return 1;
}
else if (box_area[2] && box_area[4] && box_area[6] == 'X' || 'O') {
return 1;
}
else if (box_area[0] && box_area[3] && box_area[6] == 'X' || 'O') {
return 1;
}
else if (box_area[2] && box_area[8] && box_area[5] == 'X' || 'O') {
return 1;
}
else if (box_area[0] && box_area[4] && box_area[8] == 'X' || 'O') {
return 1;
}
else if (box_area[1] && box_area[4] && box_area[7] == 'X' || 'O') {
return 1;
}
else {
return -1;
}
}
I tried out your program and found a few glitches that needed revision as well as adding in some additional bits of code just to neaten things up a bit. Following is your code with those revisions.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
char box_area[] = { '0','1','2','3','4','5','6','7','8','9' };
struct Player
{
bool turn;
char mark;
int ID;
};
void box_creat(void)
{
printf("| %c | %c | %c |", box_area[0], box_area[1], box_area[2]);
printf("\n\n");
printf("| %c | %c | %c |", box_area[3], box_area[4], box_area[5]);
printf("\n\n");
printf("| %c | %c | %c |", box_area[6], box_area[7], box_area[8]);
printf("\n"); /* Added for aesthetics */
}
void make_move(int Player_ID)
{
int choice;
printf("Player %d - please select a area between 0-9 ", Player_ID);
scanf("%d", &choice); /* Corrected missing ampersand */
printf("Player: %d makes a move\n", Player_ID);
if (choice == 0 && box_area[0] == '0') /* FYI - '0' is equivalent to integer value 48 */
{
if (Player_ID == 1)
{
box_area[0] = 'X';
}
else
{
box_area[0] = 'O';
}
}
else if (choice == 1 && box_area[1] == '1')
{
if (Player_ID == 1)
{
box_area[1] = 'X';
}
else
{
box_area[1] = 'O';
}
}
else if (choice == 2 && box_area[2] == '2')
{
if (Player_ID == 1)
{
box_area[2] = 'X';
}
else
{
box_area[2] = 'O';
}
}
else if (choice == 3 && box_area[3] == '3')
{
if (Player_ID == 1)
{
box_area[3] = 'X';
}
else
{
box_area[3] = 'O';
}
}
else if (choice == 4 && box_area[4] == '4')
{
if (Player_ID == 1)
{
box_area[4] = 'X';
}
else
{
box_area[4] = 'O';
}
}
else if (choice == 5 && box_area[5] == '5')
{
if (Player_ID == 1)
{
box_area[5] = 'X';
}
else
{
box_area[5] = 'O';
}
}
else if (choice == 6 && box_area[6] == '6')
{
if (Player_ID == 1)
{
box_area[6] = 'X';
}
else
{
box_area[6] = 'O';
}
}
else if (choice == 7 && box_area[7] == '7')
{
if (Player_ID == 1)
{
box_area[7] = 'X';
}
else
{
box_area[7] = 'O';
}
}
else if (choice == 8 && box_area[8] == '8')
{
if (Player_ID == 1)
{
box_area[8] = 'X';
}
else
{
box_area[8] = 'O';
}
}
}
int check_the_winner(void)
{
if ((box_area[0] == box_area[1]) && (box_area[1] == box_area[2]) && (box_area[0] != '0')) /* Corrected the testing for proper "and" conditioning */
{
return 1;
}
else if ((box_area[3] == box_area[4]) && (box_area[4] == box_area[5]) && (box_area[3] != '3'))
{
return 1;
}
else if ((box_area[6] == box_area[7]) && (box_area[7] == box_area[8]) && (box_area[6] != '6'))
{
return 1;
}
else if ((box_area[2] == box_area[4]) && (box_area[4] == box_area[6]) && (box_area[2] != '2'))
{
return 1;
}
else if ((box_area[0] == box_area[3]) && (box_area[3] == box_area[6]) && (box_area[0] != '0'))
{
return 1;
}
else if ((box_area[2] == box_area[5]) && (box_area[5] == box_area[8]) && (box_area[2] != '2'))
{
return 1;
}
else if ((box_area[0] == box_area[4]) && (box_area[4] == box_area[8]) && (box_area[4] != '4'))
{
return 1;
}
else if ((box_area[1] == box_area[4]) && (box_area[4] == box_area[7]) && (box_area[1] != '1'))
{
return 1;
}
else
{
return -1;
}
}
int main()
{
int return_result;
//int player_number; /* Compiler said that this wasn't being used */
int swap = 1;
int i;
struct Player player1;
struct Player player2;
struct Player *users[2]; /* Used these as address pointers to player "1" and player "2" */
users[0] = &player1; /* Before making these pointers, the users array just contained copies of the player structures */
users[1] = &player2;
(player1.mark = 'X') && (player2.mark = 'O');
(player1.turn = true) && (player2.turn = false);
(player1.ID = 1) && (player2.ID = 2);
do
{
box_creat();
swap += 1;
i = swap % 2;
if (i == 1)
{
make_move(users[1]->ID);
//box_creat(); /* Being performed at the top of the loop */
users[0]->turn = false;
users[1]->turn = true;
}
else
{
make_move(users[0]->ID);
//box_creat();
users[1]->turn = false;
users[0]->turn = true;
}
return_result = check_the_winner();
if (check_the_winner() == 1) /* Added when a winner has been sensed */
{
box_creat();
if (i == 1)
{
printf("Player 2 won!\n");
}
else
{
printf("Player 1 won!\n");
}
}
}
while (return_result != 1);
return 0;
}
I added comments to most of the places I had tweaked, but here are the highlights:
I shuffled some of the functions around so that the "main" function followed all of the helper functions (the compiler was giving warnings about the original sequence of the function positions).
I corrected the "choice" tests as "choice" is an integer, so the test of "choice" needed to be compared to an integer value (e.g. zero) as opposed to character '0' (which actually has a value of 48).
The tests for three letters in a row for either player do not work in that manner. Either a test needed to be made for each box for a certain row needed to tested for an "X" or an "O", and then if that test was true the test needed to be continued for the next box in the row, and then again for the third box in the row. In lieu of that route, the test was revised to basically say "if the character in the first box in the row is the same as the character in the second box in the row, and the character in the second box in the row is the same as the character in the third box in the row, and the first box does not contain a digit character (which means is contains an "X" or an "O"), then there is a winner.
It appears that an attempt was made to mirror the contents of "Player1" and "Player2" into an array of player structures. Although initially, structure "users[0]" contained the same data as "Player1" and structure "users[1]" contained the same data as "Player2", once the data in "Player1" and "Player2" were updated, those changes did to propagate over to the "users" array. So to utilize "users" in the spirit of this program, they were defined as pointers to their respective player structures.
Those were the most significant bits. The other bits were added for a logical conclusion of the game. Note that no logic was added for a draw.
Give that a test and see if that clarifies things and follows the spirit of the project.
First time posting here, having a problem with this code.
I want it to print no when there is more than 1 dot, for instance '2..5'.
Tried to put the following if statement:
if(num[i] == '..'){
printf("no \n);}
however with no success.
Im new to programming!
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *num = argv[1];
if (num[0] == '+' && strlen(num) >= 2 || num[0] == '-' && strlen(num) >= 2 || num[0] == '.' || (num[0] >= '0' && num[0] <= '9'))
{
for (int i = 1; i < strlen(num); i++) {
if (!(num[i] == '.' || (num[i] >= '0' && num[i] <= '9')) ) {
printf("no \n");
}
}
printf("yes \n");
} else {
printf("no \n");
}
}
}
Use this:
if(!(strcmp(num[i], "..")))
instead of
if(num[i] == '..')
Parsing numbers is not trivial. But the following works:
double parsenum(const char *num)
{
unsigned int i=0;
int neg= 1;
double result= 0.0;
int nFraction=1;
while (num[i]=='-' || num[i]=='+') {
neg= neg * (num[i]=='-'? -1 : 1);
i++;
}
while (num[i]) {
if (num[i]>='0' && num[i]<= '9') {
if (nFraction==1) {
result= result * 10 + (num[i]-'0');
}
else {
result= result + ((double)(num[i]-'0') / nFraction);
nFraction *= 10;
}
i++;
}
else if (num[i]=='.')
{
if (nFraction>1) {
printf("%s: no\n", num);
return 0.0;
}
nFraction *= 10;
i++;
}
else
{
printf("%s: no\n", num);
return 0.0;
}
}
result *= neg;
return result;
}
Test inputs:
printf("%f\n",parsenum("2..5"));
printf("%f\n",parsenum("-2.5"));
printf("%f\n",parsenum("--2.5"));
printf("%f\n",parsenum("2.5.6"));
printf("%f\n",parsenum("++2.555"));
printf("%f\n",parsenum("255.555"));
I was wondering if anyone could help me understand why only my first if statements is working. Basically, I am working on a l33t speak convertor (lol) and only my first if statement works, here is my code:
#include<stdio.h>
#include<string.h>
void translate (char blurp[]);
int main(void) {
char message[1024];
printf("enter a message: \n");
fgets(message, 1024, stdin);
translate(message);
return 0;
}
void translate (char blurp[]) {
int i;
int length;
length = strlen(blurp);
printf("\nHere it is translated: \n");
for ( i = 0; i != length; i++) {
if (blurp[i] == 'a') {
blurp[i] = '4';
printf("%c", blurp[i]);
}
else if (blurp[i] == 'b') {
blurp[i] = '8';
printf("%c", blurp[i]);
}
else if (blurp[i] == 'e') {
blurp[i] = '3';
printf("%c", blurp[i]);
}
else if (blurp[i] == 'i') {
blurp[i] = '|';
printf("%c", blurp[i]);
}
else if (blurp[i] == 'o') {
blurp[i] = '0';
printf("%c", blurp[i]);
}
else if (blurp[i] == 's') {
blurp[i] = '5';
printf("%c", blurp[i]);
}
else {
printf("%c", blurp[i]);
}
}
}
i am trying to take a string and seeing if it could be converted into decimal/float/octal/hex
I have stored these strings into an array, and im iterating through them and checking which element is what.
for(int k=0;k<i;k++){
char* string = tokenArray[k];
fprintf(newFile, "Tokens are: %s\n", string);
if(checkDecimal(string) == 1){
result[k] = "Decimal"; printf("Token: %s is %s\n", string, result[k]);
}
else if(checkFloat(string) == 1){
result[k] = "Float"; printf("Token: %s is %s\n", string, result[k]);
}
else if(checkHex(string) == 1){
result[k] = "Hex"; printf("Token: %s is %s\n", string, result[k]);
}
else if(checkOctal(string) == 1){
result[k] = "Octal"; printf("Token: %s is %s\n", string, result[k]);
}
else {
printf("Token: %s Did not work\n", string);
}
I wrote contents of my array into a separate file which is, :
fprintf(newFile, "Tokens are: %s\n", string);
Tokens are: 012
Tokens are: 23948
Tokens are: 1.21e+19
Tokens are: [
Tokens are: ,
Tokens are: 0
Tokens are: 0x56
Tokens are: 888
Tokens are: 0X11
Tokens are: 12
Tokens are: 333
Tokens are: 234
Tokens are: 012
Tokens are: 12
Tokens are: 01200
As you can see i am getting the tokens correctly, but my output is coming up weird.
This is my code for checking elements:
int checkFloat(char *s){
char *str = NULL;
long i = strtol(s, &str, 0);
if (!*str)
return 0;
if (*str == 'e' || *str == 'E' || *str == '.')
return 1;
return 0;
}
int checkHex(char *s){
char *str = s;
if((*str) == '0'){
if((*(str++) == 'x')){
printf("%s\n", "olala");
}
}
if(*(str) == 0 && (*(str++) == 'x' || *(str++) == 'X'))
{
printf("%s\n", "ok");
while(*(str) != '\0')
{
if (!isxdigit(*str))
{
return 0;
}
++str;
}
return 1;
}
return 0;
}
int checkOctal(char *s){
char *str = s;
if (*str != '0')
{
return 0;
}
while (isdigit(*str) && *str != '8' && *str != '9')
{
if(*(++str) == '\0')
{
return 1;
}
str++;
}
return 0;
}
int checkDecimal(char *s){
char *str = s;
if(*str == '0')
return 0;
for(int i=0;i<strlen(str);i++){
if(str[i] < 49 || str[i] > 57)
return 0;
}
return 1;
}
I think these functions are fine,
this is the output:
Token: 012 is Octal
Token: 23948 is Decimal
Token: 1.21e+19
is Float
Token: [ Did not work
Token: , Did not work
Token: 0 is Octal
Token: 0x56 Did not work
Token: 888 is Decimal
Token: 0X11
Did not work
Token: 12 is Decimal
Token: 333 is Decimal
Token: 234
Did not work
Token: 012
Did not work
Token: 12 is Decimal
Token: 01200 is Octal
As you can see, first token was 012 which came out as octal (fine). Another token with same 012 came out as error. Same with "234".
I dont know why my hex fucntion dosent work either.
Plz help
Your code's most outstanding issue is your use of '++' (increment). You tend to overuse it, skipping over characters you intend to test. E.g. consider a clause like:
(*(str) == 0 && (*(str++) == 'x' || *(str++) == 'X'))
Which can leave the pointer in two different locations and fails to test for 'X' as it's looking at the wrong character. This really should be:
(*str++ == '0' && (*str == 'x' || *str == 'X'))
Your code is riddled with this type of error. (As well as confusing 0 with '0' as noted in the comments.) Fixing your increments and generally reworking your code:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
typedef enum { FALSE = 0, TRUE } boolean;
int checkFloat(char *s) {
char *extra = NULL;
(void) strtol(s, &extra, 0);
return ((*extra != '\0') && (*extra == 'e' || *extra == 'E' || *extra == '.'));
}
int checkHex(char *s) {
char *str = s;
if (*str++ == '0' && (*str == 'x' || *str == 'X')) {
while (*(++str) != '\0') {
if (!isxdigit(*str)) {
return FALSE;
}
}
return TRUE;
}
return FALSE;
}
int checkOctal(char *s) {
char *str = s;
if (*str++ != '0') {
return FALSE;
}
while (isdigit(*str) && *str != '8' && *str != '9') {
if (*(++str) == '\0') {
return TRUE;
}
}
return FALSE;
}
int checkDecimal(char *s) {
char *str = s;
if (*str == '0') {
return FALSE; // looks like octal
}
for (size_t i = 0; i < strlen(str); i++) {
if (str[i] < '1' || str[i] > '9') {
return FALSE;
}
}
return TRUE;
}
char *tokenArray[] = {
"012",
"12948",
"1.21e+19",
"[",
",",
"0",
"0x56",
"888",
"0X11",
"12",
"333",
"234",
"012",
"12",
"01200"
};
#define TOKEN_COUNT (sizeof(tokenArray) / sizeof(char *))
int main() {
char *result[TOKEN_COUNT];
for (size_t k = 0; k < TOKEN_COUNT; k++) {
char *string = tokenArray[k];
if (checkDecimal(string)) {
result[k] = "Decimal";
printf("Token: %s is %s\n", string, result[k]);
} else if(checkFloat(string)) {
result[k] = "Float";
printf("Token: %s is %s\n", string, result[k]);
} else if (checkHex(string)) {
result[k] = "Hex";
printf("Token: %s is %s\n", string, result[k]);
} else if (checkOctal(string)) {
result[k] = "Octal";
printf("Token: %s is %s\n", string, result[k]);
} else {
printf("Token: %s Did not work\n", string);
}
}
return 0;
}
Produces:
Token: 012 is Octal
Token: 12948 is Decimal
Token: 1.21e+19 is Float
Token: [ Did not work
Token: , Did not work
Token: 0 Did not work
Token: 0x56 is Hex
Token: 888 is Decimal
Token: 0X11 is Hex
Token: 12 is Decimal
Token: 333 is Decimal
Token: 234 is Decimal
Token: 012 is Octal
Token: 12 is Decimal
Token: 01200 is Octal
In general, style-wise, you should pick an indentation/bracketing style you like and stick with it consistenly -- your code is all over the place.