The reason that gets() is dangerous is that it doesn't do any bounds checking on input values. You have allocated string as an 85-byte array of char; what will happen, do you think, if someone enters 85 or more characters? gets() will merrily start overwriting parts of memory that are crucial to the operation of your program.
–
larsksAug 7 '12 at 13:12

1

gets() is also removed from the C language since C11.
–
LundinAug 7 '12 at 13:15

1 Answer
1

In order to store the various substrings, you will need an array of char pointers. As you separate out your substrings, which you are copying into a malloced memory area, you take the pointer that is returned and store the pointer into your array of char pointers.

So something like the following will define an array of chars along with a count of the number of char pointers that are currently in the array.

char *pArrayStrings [100];
int iArrayStringsIndex = 0;

At this point you will have an array of char pointers. You can then do a search through the array to determine if the palindrome has already been found. Something like the following.

{
int iLoop = 0;
// search the array to see if this palindrome is already there
for (iLoop = 0; iLoop < iArrayStringsIndex; iLoop++) {
if (strcmp (pArrayStrings[iLoop], pointer) == 0) {
// found a match for this palindrome
break;
}
}
if (iLoop >= iArrayStringsIndex) {
// this is a new palindrome that is not in the array
pArrayStrings[iArrayStringsIndex] = pointer;
iArrayStringsIndex++;
}
}

The above strcmp() function is case sensitive so you might want to use the case insensitive compare.

I have not tried compiling this code so there may be an error in it however this would be a general approach that should get you close to what you want.

Once you are done, you can then loop over the array and do a free() to free the malloced memory.

hey bro, thanks very much. i get your logic very well. but you had a slight mistake in your code. pArrayStrings[iArrayStringsIndex] = pointer; is not valid. so i replaced it with pArrayStrings[iArrayStringsIndex]=malloc(length+1); strcpy(pArrayStrings[iArrayStringsIndex],pointer); now it works perfect
–
Nasif Imtiaz OhiAug 7 '12 at 21:02