0

I wrote this code in C, where I wanted to extract words from str, then store them in char word[] (one by one), and send it to another function- palindrome. However, the words are not being formed properly. I'm new to this language so I don't want to use pointers or something else. I want to do it in the most simple way possible. Could you please suggest modifications to the code so that the words get formed properly?

int main()
{
    char str[100];
    int a=0, l, p=0;
    printf("Enter the text \n");
    gets(str);
    l=strlen(str);
    for(int i=0;i<l;i++)
    {
        char word[100]; int a=0;
        if(str[i]==' '||str[i]=='\0')
        {
            for(int j=p;j<i;j++)
            {
                word[a]=str[j];
                a++;
            }
        printf("This \n");
        puts(word);
        palindrome(a,word);
    }
}

return 0;
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Anushka
  • 1
  • 1
  • Don't use gets another time ,Please read this :https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used – MED LDN Dec 13 '20 at 10:47
  • You can read string like this : scanf("%[^\n]s", str); – MED LDN Dec 13 '20 at 10:50
  • *I don't want to use pointers* , the array syntax it self is a form of ***pointer arithmetics***... – alex01011 Dec 13 '20 at 10:58

2 Answers2

0

Basic things in C don't mean easy to understand. I advise you to understand this code well in order to understand why using high level functions while being a beginner is not necessarily a good thing.

#include <unistd.h>

int main()
{
    char buffer;
    
    while(read(0, &buffer, 1) > 0)
        write(1, &buffer, 1);
    return 0;
}
sapristi12
  • 82
  • 9
0

Since you are an enthusiastic learner and have already tried to solve a problem, the best way to help you is to provide you the guidelines which will assure that you will end up solving your issue successfully.

Pointers

You do not want to use pointers. But what pointers are? Nothing more than variables pointing to a certain position in memory, being aware of the type. You already use arrays, but arrays are memory sections having a type and a size. That section has a start address, so your array is actually not very different from a pointer. So, the intent to avoid using pointers was already breached by the use of arrays, but I think I understand what you want. You want to minimalize pointer arithmetics.

Understandable, implementable solution

You can iterate the characters of your array and store the first non-space character index since the last space you have encountered and find the next space. At that point, you can create a new array of a size which matches the number of characters in that interval and copy the characters from the interval question into that array. Since you are learning C, I will provide you an algorithm that you can implement (this is not actual program code)

wordStart <- -1
lastSpace <- -1
index <- 0

    while (index < sizeof(input)) do
        if (input[index] != ' ') then
            if (wordStart == lastSpace) then
                wordStart <- index
            end if
        else
            if (wordStart == lastSpace) then
                wordStart <- index
                lastSpace <- index
            else
                lastSpace <- index
                //create array of characters, having lastSpace - wordStart elements
                //copy the elements between the index of lastSpace and wordStart - 1 into your new array
                //pass that new array into your call to palindrom
            end if
        end if
        index <- index + 1
    end while
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175