Thread: Concatenate palindrome words from a string to another string

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    69

    Concatenate palindrome words from a string to another string

    Merry Christmas! I'm having some trouble with a problem regarding strings. The problem statement is:
    Implement a function that receives a C-styled string (s) and an array of characters (buff) together with its capacity
    and concatenates into buff all the words from s which are palindromes(we consider a word to be a palindrome if read from left to right is the same as being read from right to left; i.e. "hannah" is such an example).
    Avoid overflowing the array.
    A word is a sequence of non-whitespace chars separated by at least one whitespace char (the first and the last word might miss the leading or trailing whitespaces).
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    unsigned checkWord(char word[])
    {
        for(int i = 0; i <= strlen(word); i++)
        {
            if(isspace(word[i]))
                return 0;
        }
        return 1;
    }
    unsigned checkPalindrome(char str[])
    {
        int l = 0;
        int h = strlen(str) - 1;
        while (h > l)
        {
            if (str[l++] != str[h--])
            {
                return 0;
            }
        }
        return 1;
    }
    void concatenatePalindromes(char s[], char buff[], int cap)
    {
        cap = 0;
        char* word = strtok(s, " ");
        while(word != NULL)
        {
            if(checkWord(word) == 1 && checkPalindrome(word) == 1)
            {
                strcat(buff, word);
            }
            word = strtok(NULL, " ");
        }
        cap = '\0';
        printf("%s", buff);
    }
    int main()
    {
        char s[] = "ana john michael hannah ana stacy";
        char buff[] = ""; //anahannahana
        concatenatePalindromes(s, buff, 40);
        return 0;
    }

    So I'm getting the right output(anahannahana), but I have a question about the "Avoid overflowing the array part". How do I do this? In my code I tried setting the cap to '\0' in the end, but I'm quite sure that's not it. Could you please explain?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,595
    Well you are definitely overflowing your destination array.

    You are also passing a "cap" variable to your function that is larger than either of your arrays, s[] has a size of 33 and buff has a size of 1, yet your "cap" is 40.

    Your destination array must be large enough to hold the concatenated string plus the end of string character.

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    69
    Quote Originally Posted by jimblumberg View Post
    Well you are definitely overflowing your destination array.

    You are also passing a "cap" variable to your function that is larger than either of your arrays, s[] has a size of 33 and buff has a size of 1, yet your "cap" is 40.

    Your destination array must be large enough to hold the concatenated string plus the end of string character.
    Then how can I avoid overflowing? I just put 40 in there as a parameter to see if the program works, what should I change in order for it not to overflow?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,595
    Well you may want to start by allocating an array large enough to hold the contents of your concatenated strings, and passing the correct capacity value (probably the size of one of the arrays, not some random number).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I can't concatenate a string
    By qqsszz in forum C Programming
    Replies: 9
    Last Post: 06-29-2018, 03:55 PM
  2. Replies: 10
    Last Post: 12-09-2011, 02:52 AM
  3. Concatenate string and int
    By millsy5 in forum C Programming
    Replies: 1
    Last Post: 01-28-2010, 04:43 AM
  4. Concatenate a String?
    By sharkbate24 in forum C++ Programming
    Replies: 7
    Last Post: 01-02-2009, 05:16 PM
  5. concatenate an integer to a string?
    By barneygumble742 in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2005, 05:24 AM

Tags for this Thread