I have a little problem related to the white spaces in my program. Well, the actual program I'm trying to make is something else. so this program is just to check whether the text is properly broken or not.
The problem I'm facing is, for the first word in my text, the node is made properly. but for the second word, if the string length is less than the first word, the compiler even considers the 'space'. I don't know why it is happening!! Can anyone help me out in this?
Here's the program:

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
    char word[10];
    struct node* next;
}*HEAD=NULL,*TAIL=NULL;
void element(int,char *);
int main()
{
    char text[100],word[10]; int i=0,j;
    printf("Enter the text here: \n");
    gets(text);
    for(;text[i]!='\0';i++)
    {
        for(j=0;text[j]!=' ';j++,i++)
         {
            word[j]=text[i];
         }
        word[j]='\0';
        element(i,word);
    }
    getch();
     return 0;
}

void element(int x,char *y)
{
    int m=0;
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    while(m<=x)
     {
        tmp->word[m]=*(y+m);
        m++;
     }
    tmp->word[m]='\0';
    printf("%s ",tmp->word);
}

Dani AI

Generated

Quick diagnosis: two separate bugs cause the symptom you describe. The inner loop is testing the wrong index (so it reads the wrong bytes) and the code passes the outer index value into element instead of the actual length of the extracted word. That makes the copier copy extra bytes (including the space). was right to flag the loop index and to recommend safer input — those are the immediate fixes, but there are a few more practical points to avoid similar problems.

Concrete fixes (no code shown here): parse the input by advancing a single index over text (skip leading spaces first), then collect characters into word while the current character is neither space nor NUL and while word has room. Terminate word with NUL and call your node-creation routine passing either the string itself or the actual word length (not the global i position). After consuming a word, advance the index past any extra spaces before the next iteration. This removes the double-increment/offset confusion your current nested for loops create.

What element must do differently: treat its string parameter as a finished C string (or accept a length) and copy only up to that length; always NUL-terminate the node buffer; check buffer sizes to avoid overflow; check malloc return; set tmp->next = NULL and update HEAD/TAIL (if HEAD is NULL set both, otherwise append and update TAIL). Right now element prints the buffer but never links nodes and copies using a wrong bound.

Extra tips: stop using unsafe input functions and nonstandard console headers; test edge cases (multiple spaces, leading/trailing spaces, very long words, empty input); instrument with simple prints or a debugger to watch i, j, and extracted word; use a memory checker (valgrind) to catch overruns. These checks will make the parsing robust and fix the space-carrying bug.

line 18: should be text[i] != ' '. You used the wrong loop counter.

After line 23 you should advance i counter until text[i] != space so that there can be more than one space between words

line 15: gets() is a bad function to use becuse it will allow you to type more characters then the buffer can hold, causing your program to crash. Instead of gets() use fgets() so that input length of limited to no more than buffer size. fgets(text, sizeof(text), stdin);

tmp->word[m]=*(y+m);

lines 34-38: Too compilicated. Just call strcpy() which doesn't need as loop. Unless of course if you are not allowed to use strcpy()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.