'How to Identify if an input string is an int, float or char in C
I'm working on a code where I use linked list to accept a string data and put it into a main list and specific list where the data belongs. I'm having trouble on how do I do it as I've tried but output is different of what is expected. There are no errors, however. If anyone would try to look and help me I would gladly apprectiate it. Thank you....
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
struct data
{
char info[10];
struct data *prev;
struct data *next;
};
typedef struct data* nodePtr;
nodePtr CreateData(char info[])
{
nodePtr newData = (nodePtr)malloc(sizeof(struct data));
strcpy(newData->info, info);
newData->next = newData->prev = NULL;
return newData;
}
void AddData(nodePtr *head, nodePtr *tail, nodePtr *headInt, nodePtr *tailInt, nodePtr *headFloat, nodePtr *tailFloat, nodePtr *headChar, nodePtr *tailChar, char info[])
{
int flag = 0;
int loopVar;
nodePtr newData = CreateData(info);
if(*head == NULL)
{
*head = *tail = newData;
(*head)->prev = NULL;
(*tail)->next = NULL;
}
else
{
(*tail)->next = newData;
newData->prev = *tail;
*tail = newData;
(*tail)->next = NULL;
}
loopVar = 0;
while(info[loopVar++] != '\0')
{
if(info[loopVar] == '.')
{
flag = 1;
break;
}
}
if(flag == 1 && atof(info) != 0)
{
if(*headFloat == NULL)
{
*headFloat = *tailFloat = newData;
(*headFloat)->prev = NULL;
(*tailFloat)->next = NULL;
}
else
{
(*tailFloat)->next = newData;
newData->prev = *tailFloat;
*tailFloat = newData;
(*tailFloat)->next = NULL;
}
}
else if(flag == 0 && atoi(info) != 0)
{
if(*headInt == NULL)
{
*headInt = *tailInt = newData;
(*headInt)->prev = NULL;
(*tailInt)->next = NULL;
}
else
{
(*tailInt)->next = newData;
newData->prev = *tailInt;
*tailInt = newData;
(*tailInt)->next = NULL;
}
}
else if(atoi(info) == 0 && atof(info))
{
if(*headChar == NULL)
{
*headChar = *tailChar = newData;
(*headChar)->prev = NULL;
(*tailChar)->next = NULL;
}
else
{
(*tailChar)->next = newData;
newData->prev = *tailChar;
*tailChar = newData;
(*tailChar)->next = NULL;
}
}
}
void DisplayData(nodePtr head)
{
nodePtr current = head;
while(current != NULL)
{
printf("%s ", current->info);
current = current->next;
}
printf("\n");
}
void main()
{
nodePtr head = NULL;
nodePtr tail = NULL;
nodePtr tailInt = NULL;
nodePtr headInt = NULL;
nodePtr tailFloat = NULL;
nodePtr headFloat = NULL;
nodePtr tailChar = NULL;
nodePtr headChar = NULL;
AddData(&head, &tail, &headInt, &tailInt, &headFloat, &tailFloat, &headChar, &tailChar, "1.2");
AddData(&head, &tail, &headInt, &tailInt, &headFloat, &tailFloat, &headChar, &tailChar, "1.1");
AddData(&head, &tail, &headInt, &tailInt, &headFloat, &tailFloat, &headChar, &tailChar, "123");
AddData(&head, &tail, &headInt, &tailInt, &headFloat, &tailFloat, &headChar, &tailChar, "2");
AddData(&head, &tail, &headInt, &tailInt, &headFloat, &tailFloat, &headChar, &tailChar, "A");
DisplayData(head);
DisplayData(headFloat);
DisplayData(headInt);
DisplayData(headChar);
}
Solution 1:[1]
You said:
but output is different of what is expected
Well, I am not sure what the "expected" here is exactly defined, but you code seems to try to insert different types of string into different list and display them. I notice you also display the main list(i.e. DisplayData(head)). If you want each node to appear on two lists: the main list and its type list, you need at least two pairs of pointer. For example:
struct data
{
char info[10];
struct data *prev;
struct data *next;
struct data *main_prev;
struct data *main_next;
};
The extra main_* pointer pair will serve to make each node appear on the main list.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kirhhoff |
