- Say I have the following values in the double linked list: "Orange, banana, PEAR".
- How can I successfully convert each of those strings into: "ORANGE, BANANA, PEAR".
The for loop I used to convert the lowercase char to uppercase char is supposed to run until it reaches the null char of the string in head->data, and then check if its a lowercase converting it to an upper case by substracting the char at that index by 32 to get the uppercase ASCII value of the lowercase char. A friend suggested to type cast it, but even then it won't fully execute it kept giving me same output. So I'm stuck as to what could be the issue. Any help is appreciated or suggestions.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//My node structure
struct node
{
char* data;
struct node *prev;
struct node *next;
};
//Declared two global variables
struct node* head = NULL;
struct node* tail = NULL;
// Function to insert at the front
// of the linked list
void insertAtEnd(char* data) {
//Create a new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
//If list is empty
if(head == NULL) {
//Both head and tail will point to newNode
head = tail = newNode;
//head's previous will point to NULL
head->prev = NULL;
//tail's next will point to NULL, as it is the last node of the list
tail->next = NULL;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail->next = newNode;
//newNode's previous will point to tail
newNode->prev = tail;
//newNode will become new tail
tail = newNode;
//As it is last node, tail's next will point to NULL
tail->next = NULL;
}
}
// The traverse list function
void traverse()
{
// List is empty
if (head == NULL) {
printf("\nList is empty\n");
return;
}
// Else print the Data
struct node* temp;
temp = head;
while (temp != NULL) {
printf("%s\n", temp->data);
temp = temp->next;
}
}
int main(int argc, char **argv){
insertAtEnd("Orange");
insertAtEnd("banana");
insertAtEnd("PEAR");
//converting each char in head->data into uppercase letter.
for (int i = 0; head->data[i]!='\0'; i ) {
printf("Entered outer loop\n");
if((int)head->data[i] >= 97 && (int)head->data[i] <= 122) {
head->data[i] = (char)(((int)head->data[i]) - 32);
printf("Entered inner loop\n");
}
}
traverse();
}
Expected output(after attempting to convert each char to uppercase and calling traverse()):
ORANGE
BANANA
PEAR
Actual output:
Entered outer loop
Edit: joined all the code together for better reproduction and readability. My goal overall is to get to update the strings saved in the double linked list to become fully uppercase without using the uppercase function. Hence, that is why I ended up trying to use that for loop which supposedly uses the ASCII values in the table as reference to convert from lowercase to uppercase. However, when I run it for some reason it is not executing
CodePudding user response:
You pass const char* to data so you can't convert it, you should write like this:
struct node* tmp = head;
while(tmp) {
tmp->data = strdup(tmp->data);
for (int i = 0; tmp->data[i]!='\0'; i ) {
if(tmp->data[i] >= 97 && tmp->data[i] <= 122) {
tmp->data[i] = tmp->data[i] - 32;
}
}
tmp = tmp->next;
}
traverse();
And you no need to cast char to int, char can be used like an integer