Here is a function for some code im working on in C. Right now it accepts 2 different user inputs and stores them as city and distance. What I want is for the user to input only once. For example "Enter the city name and distance from previous city: Arlington 200". I need to separate that string into values for city and distance. I think I need to use tokens but im not sure how.
void addCity ()
{
char city[30];
int distance;
printf("Enter city name : ");
scanf("%s",city);
printf("Enter distance from previous city: ");
scanf("%d",&distance);
printf("The city has been added.\n");
if(root == NULL){
root = (struct node *) malloc( sizeof(struct node) );
strcpy(root->city, city);
root->distance = distance;
root->next = NULL;
}
else
{
struct node *currentNode = root;
struct node *newNode = (struct node *) malloc( sizeof(struct node)
);
while(currentNode->next!=NULL)
{
currentNode = currentNode->next;
}
strcpy(newNode->city, city);
newNode->distance = distance;
currentNode->next = newNode;
}
}
Hello!, Yes you can use tokenize the string using a delimiter. It is a common approach used by developers all around the world. But you will have to be careful when implementing tokenizing since it could easily cause problems when inputting something that is not excepted from the user. I have written a small piece of code so that you understand how to implement tokenization in your code. You can use strtok function in C. It is used for tokenization of the string using delimiters. I have explained the entire code in the comments within the code so that you can understand it easily. Please ask if you have any doubts. I am always available to help you. have a nice day :)
==========tokenizing code using strtok===================
#include <string.h>
#include <stdio.h>
#include <stdlib.h> // we need strlib for using atoi.
int main () {
char city_and_distance[40] = "Arlington 200";
int distance;
char city[30];
const char s[2] = " ";
char *token;
/* You can use strtok to tokenize the string based on the
delimiters.
The syntax is - char *strtok(char *string1, const char
*string2);
*/
token = strtok(city_and_distance, s);
strcpy(city, token);
/* get the rest of the tokens. We are basically
looping over the tokens until we obtain a null value.
Basically strtok returns the pointer of the
first token when it counters the delimiter. Then we have to
loop it over to obtain the rest of the tokens.
When it searches for the first token it skips over the rest of
the
leading delimiters. And then it returns the pointer. When strtok
is
called with a null for string1 argument, the next token is
read
from the stored copy of the last non null string1. Then each of
the
delimiters is replaced with a null character. */
while( token != NULL ) {
distance = atoi(token); //atoi is basically to convert the string
to an integer.
token = strtok(NULL, s);
}
printf("%d\n%s\n", distance , city);
return(0);
}
======================End of tokenizing code====================


As you can see in the above output I have tokenized the string based on a space. You can do the same using any delimiter.
I have also modified your code so that you get an idea of how to implement it into your code.
==================modified code start===============================
void addCity ()
{
char city[30];
int distance;
char city_and_distance[40];
const char s[2] = " "; /* s[2] is the delimiter. you can put any
delimiter like ,
or $ for seperating the city and distance.
or you can use a simple space
*/
char *token;
printf("Enter city name and the distance: ");
scanf("%s",city_and_distance);
token = strtok(city_and_distance, s);
strcpy(city, token);
while( token != NULL ) {
distance = atoi(token); //atoi is basically to convert the string
to an integer.
token = strtok(NULL, s);
}
printf("The city has been added.\n");
if(root == NULL){
root = (struct node *) malloc( sizeof(struct node) );
strcpy(root->city, city);
root->distance = distance;
root->next = NULL;
}
else
{
struct node *currentNode = root;
struct node *newNode = (struct node *) malloc( sizeof(struct node)
);
while(currentNode->next!=NULL)
{
currentNode = currentNode->next;
}
strcpy(newNode->city, city);
newNode->distance = distance;
currentNode->next = newNode;
}
}
===============================Modified code end================================

I assume that this is the way that you implement tokenizing in your code. of course it looks a bit redundant to add tokenization twice. But I have made it so that there is a clear distinction between reading city and distance for your understanding.
Also, I have earlier mentioned about handling the user input can be problematic when using delimiters. The reason is simple. Right now the distance is being read as a string and then being converted into an integer. What if the city had a space like Los Angeles, and the input was Log Angeles 200. Then the program would take the second token which is Angeles and the program would crash. To counter this you can use a delimiter such as "," to separate the city and distance. But if you ask me then the best approach would be to identify the type of the string, i.e if the string or character is a number or not. If it is then store it in distance else store it in city. This is make it more better and you have very less chances of encountering the error. This can be done by using isdigit() which returns 0 if the string value is in [A-Z] or else it returns a random number.
Here is a function for some code im working on in C. Right now it accepts...
****Using C and only C****
I have some C code that has the function addRecord, to add a
record to a linked list of records. However, when I run it, the
program exits after asking the user to input the address. See
picture below:
Here is my code:
#include<stdio.h>
#include<stdlib.h>
struct record
{
int accountno;
char name[25];
char address[80];
struct record* next;
};
void addRecord(struct record* newRecord) //Function For Adding
Record at last in a SinglyLinkedList
{
struct record *current,*start,*temp;...
Write a function to insert a name at the end of a linked list? (C) I have a linked list: struct node { char person[100]; struct node *next; }; int main() { struct node *new_node; new_node=malloc(sizeof(struct node)); printf("Please enter a name:\n"); scanf("%s", ); } How do I get the name from the user and write a function to add it to the end of the linked list? I'm not supposed to use the insert() library function, which is why I'm...
Hi, I need to make a program in C that reads the type of currency and organizes them into stacks based on currency which can be read back. This is what I have so far, can I get help finishing it? #include <stdio.h> #include <stdlib.h> const float POUND = 1.31; const float YEN = 0.0091; const float RUPEE = 0.014; const float EURO = 1.11; char c; int currValue; float exchangeValue; float finValue; int printValue; struct node { int...
Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...
Modify the below code to fit the above requirements:
struct node
{
char data;
struct node *next;
struct node *previous;
} *front, *MyNode, *rear, *MyPointer, *anchor *Valuenode ;
typedef struct node node;
int Push(char input)
{
if(IsFull()==1)
{
printf("The queue is full. Enter the ‘^’ character to
stop.\n");
return -1;
}
else if (IsFull()==-1)
{
node *MyNode=(node*)malloc(sizeof(node));
MyNode->data=input;
rear->next=MyNode;
MyNode->previous=rear;
MyPointer=rear=MyNode;
return 1;
}
else
{
node *MyNode=(node*)malloc(sizeof(node));
node *anchor=(node*)malloc(sizeof(node));
MyNode->data=input;
MyPointer=rear=front=MyNode;
MyNode->previous=NULL;
MyNode->next=NULL;
anchor->next=MyNode;
return 0;
}
}
char...
I am stuck on a data structure problem, I am just going off of Geeks for Geeks for help but I need to print 100 random numbers in the range of [1-200]. I can currently only print 5 numbers. Here is my code: #include <stdio.h> #include <stdlib.h> #include <limits.h> using namespace std; //binary tree has data & left and right child struct node{ int data; struct node *left; struct node *right; }; //create a new node struct node* newNode (int...
Need some help creating a bubble sort for my linked list in C. Here are the structs used in my code: struct simulation { void *list; int et; /* in seconds */ }; struct plane { double x, y, altitude; char callsign[15]; struct simulation *sim; }; Here is the struct used in my linked list: struct Node { void *data; struct Node *next; }; Here is my insert function: //ComparisonFunction and FILE not...
Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that holds ints instead of strings. In your main, insert 25 to 75 random integers with range (0 to 100) into a linked list of your nodes. Use a random to determine how many to make. Write a function int sum(NodePointer current) that returns the sum of the integers in the list by looping through the linked list. Write a function int count(NodePointer current) that...
I am having problems with the following assignment. It is done
in the c language. The code is not reading the a.txt file. The
instructions are in the picture below and so is my code. It should
read the a.txt file and print. The red car hit the blue car and
name how many times those words appeared. Can i please get some
help. Thank you.
MY CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
char *str;
int...
C LANGUAGE I just need the void push() function, which inserts new node to the front of the list #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } Node; //Creating head a as a global Node* Node *head; /* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter (Node * prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf ("the given...