+ 2
Why the output is like ggggggP@?
19 Answers
+ 9
#include <stdio.h>
#include <string.h>
int main() {
char *s = "string";
int len = strlen(s);
for (int i = 0; i < len; i++) {
printf("%c", s[i]);
}
return 0;
}
/* I believe this is what you wanted... */
+ 3
Because in: s[len-1] you always take the same value, the 'g' on 'string'. And the final, P@, its because you dont use any value( the 'for' dont go until 10)
+ 3
Hima
You're welcome, and about your code;
Your char array <p> is not initialized, and as such it contains garbage values, whatever value was left at the time memory allocation was requested in order to create the array.
In your loop, you copy the last character from <s> into <p> for <len> times (6 times). But the remaining garbage values (4 characters) are not modified, because your loop only goes six times (length of <s>) not ten times (length of <p>).
If you're wondering why it shows less than 10 characters on output, then know that there are non printable characters. One of the characters in <p> may be a non printable character, considering the char array wasn't initialized and carrying garbage values.
Hope this clears the doubt a bit.
(Edited).
+ 2
the output is like ggggggP@ But should it not be a string of 9 characters
+ 2
There's a space character between P and @
+ 2
Hima
If you meant to put the reversed word of "string" into <p>, I have two points to note. But just never mind if it was not what you had in mind.
1. Initialize the <p> char array to zero (or null char) since it's a string.
char p[10] = {0};
This ensures you can have a valid string once you're done copying characters in reverse from <s> to <p>.
2. Your loop is not correctly modifying the proper character in <p> to achieve the reverse effect. Try to use this in your loop
p[i] = s[len - i - 1]
+ 2
Ipang okay. like you said I wasn't asking what you answered . Anyways, ty for taking a minute of you
+ 1
so why only 8 characters? Why not 9?
+ 1
Can you explain what you are trying to achieve from this code Hima ?
+ 1
Haha
+ 1
stefani santos Man ! I was too reckless to see that . Getting dumb of myself .
+ 1
š
+ 1
Hima I thought comments before mine had already made it clear about what was wrong. I just suggested what could have been done to do what u wanted.
As for the code ,
Your p array was initialized with garbage values, i.e.
p = { @, , , , , , , P, ā, @ }
, When you started copying from index 0 to 5th , you assigned the last character of "string" i.e. 'g' to each of those indexes.
After that your for loop termined and your resultant array was
p = { g,g,g,g,g,g,P,ā,@)
which you printed.
0
That doesn't answer my question
0
Ipang I just couldn't see through my mistake. just a random thing
0
Just add a len- - statement after i++ in your loop and change condition to i<10. It will print the reverse.
0
MOHD SHADAN KHAN that's not what the question was
0
Here you declared char p[10]; so it can contain 10 char from 0 to 9 and it is all unintialized that means it contain garbage, then you have int len that will contain 6 because it's the length of the char s*; after that in the loop that goes from 0 to 5 you always copy s[len-1] which is 6-1 = 5 which is the the letter "g" in to p[i] which goes from 0 to 5 then you print the string p and used %s put this will print the list of characters until the end of string mark which is '\0' which you didn't add after the last 'g' you copied in the string p so you see garbage after the last letter because it continue printing what ever it finds in the memory that why you find @ in the end.