Is this a bug?

Joined
May 27, 2018
Messages
1
Reaction score
0
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:");
gets(cadena3);
tamanodc=strlen(cadena3);
for(i=0;i<tamanodc;i++){
letradc=cadena3;
if(letradc!=' '){

cese1[numdecece]=cadena3;
numdecece++;
}

}
tamanodeces=strlen(cese1);
printf("%i",tamanodeces);
for(f=0;f<tamanodeces;f++){
printf("lo lograste");
}
system("pause");
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
 
Joined
Jun 27, 2018
Messages
12
Reaction score
5
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:

[code=c]
// post your code here
[/code]

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:
C:
letradc=cadena3;
AND
C:
cese1[numdecece]=cadena3;
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:
C:
letradc=cadena3[i];
and
C:
cese1[numdecece]=cadena3[i];
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:
C:
for(int i=0; i<tamanodc;++i)
{
    if(cadena3[i]!=' ')
    {
        cese1[numdecece] = cadena3[i];
        ++numdecece;
    }
}
// Append a null terminator character to the end of the copied string
cese1[numdecece]='\0';

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

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top