Is this a bug?

May 27, 2018
Reaction score
So im currently making a program that analyze a string and detects whether if written backwards is the same as if its written normally(palindrome) so i made the code using strlen and 2 for loops, 1 to make the string without spaces and the other one to analyze whether its a palindrome, everything is fine but when the code reaches to the second for loop the strlen of the second string(the one without spaces) shows a wrong number preventing me from analyzing the string. Here is the code:

char cadena3[100];
int tamanodc=0;
char letradc;
int numdecece=0;
char cese1[100];
int f;
int tamanodeces;
printf("Ingrese Cadena para verificar:");
if(letradc!=' '){


printf("lo lograste");
when i get to this point, if i write a 3 caracter string the for loop somehow causes it to be another number. i dont understand


Jun 27, 2018
Reaction score
I'm a little late to the party here, I assume you've already got an answer from elsewhere.
But as it has had no replies - I will answer the question anyway.

The first thing I will say is:
"In future, please post ALL code inside code tags."
It will make your code easier to read because it will have syntax highlighting and all of the formatting/indentation of the code will be preserved. To put your code inside code tags, you can write this in your post:

// post your code here

You can also use the 'insert' button on the post editors toolbar (the icon to the left of the "floppy-disk"/draft icon). Click insert, then select "code" and a dialog will pop up where you can post your code and you can specify the programming language used.

Your failure to use code tags makes your code look like it has more bugs than it actually does.

For example - These lines of code:
Are both appearing incorrectly in your post because you didn't use code tags.
So the line: "letradc=cadena3[i];" is appearing as "letradc=cadena3;" because the array index "[i]" is being interpreted by thecodingforums text editor as a tag for italics. Which is why the semicolon at the end of the line and all of the rest of your post is in italics.

And because of this - to anybody reading the code you posted, it looks as if you have a type-mismatch in those two assignment operations. Because the two types do not match.
e.g. letradc is declared as a char and cadena3 is an array of char.
Attempting to assign an entire array of char to a single char should result in a compiler error.

However - in the code you posted, I believe you are correctly using cadena3[i] and NOT cadena3 in those two assignments. So these bugs are just false-positives because you failed to use code-tags in your post.

Also, in this post - the reason that instances of [i] are NOT being interpreted as italics tags is because I have enclosed them all in PLAIN tags.

So I'm guessing that the lines you pasted were originally:
Which are both valid and correct assignments.

So ignoring all of the false-positives that were caused by your failure to use code tags in your post - the REAL bug in your code is becuase the copied string "cese1" doesn't have a null terminator at the end of it, which is why you are getting the wrong size reported for it.

To fix the problem you need to add a null character '\0' to the end of the copied string in order to properly terminate it. You should do this immediately AFTER the first for loop.

Also, you can refactor the logic of the first loop to completely remove the "letradc" variable, there is no point using a variable here. You can simply compare cadena3[i] against a space character.

So, applying the fix - your first for loop should look something like this:
for(int i=0; i<tamanodc;++i)
    if(cadena3[i]!=' ')
        cese1[numdecece] = cadena3[i];
// Append a null terminator character to the end of the copied string

Now your for loop will copy all of the non-space characters from cadena3 to cese1 and will correctly terminate the string. And you will get the correct output from your second for loop. So the final thing to do would be to implement some code to check whether or not the copied string is palindromic.

Again, for future reference - please post ALL code inside code tags!

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Crossword 14
Parking lot C# 0
C: Dynamic string using variable number of arguments 2
C programming assignment 0
Help with my program?? 2
Crossword 2
Fibonacci C code 1
Parsing cdata using expat in C 0