18th Nov 2019, 9:04 AM
Hima
Hima - avatar
18 Respostas
+ 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... */
18th Nov 2019, 9:15 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 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)
18th Nov 2019, 9:33 AM
stefani santos
+ 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).
18th Nov 2019, 10:44 AM
Ipang
+ 2
the output is like ggggggP@ But should it not be a string of 9 characters
18th Nov 2019, 9:39 AM
Hima
Hima - avatar
+ 2
There's a space character between P and @
18th Nov 2019, 9:46 AM
stefani santos
+ 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]
18th Nov 2019, 9:59 AM
Ipang
+ 2
Ipang okay. like you said I wasn't asking what you answered . Anyways, ty for taking a minute of you
18th Nov 2019, 10:02 AM
Hima
Hima - avatar
+ 1
so why only 8 characters? Why not 9?
18th Nov 2019, 9:37 AM
Hima
Hima - avatar
+ 1
Can you explain what you are trying to achieve from this code Hima ?
18th Nov 2019, 9:40 AM
Ipang
+ 1
Haha
18th Nov 2019, 9:47 AM
Hima
Hima - avatar
+ 1
stefani santos Man ! I was too reckless to see that . Getting dumb of myself .
18th Nov 2019, 9:49 AM
Hima
Hima - avatar
+ 1
šŸ˜…
18th Nov 2019, 9:50 AM
stefani santos
+ 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.
19th Nov 2019, 8:01 PM
MOHD SHADAN KHAN
MOHD SHADAN KHAN - avatar
0
That doesn't answer my question
18th Nov 2019, 9:18 AM
Hima
Hima - avatar
0
Ipang I just couldn't see through my mistake. just a random thing
18th Nov 2019, 9:50 AM
Hima
Hima - avatar
0
Just add a len- - statement after i++ in your loop and change condition to i<10. It will print the reverse.
19th Nov 2019, 7:32 PM
MOHD SHADAN KHAN
MOHD SHADAN KHAN - avatar
0
MOHD SHADAN KHAN that's not what the question was
19th Nov 2019, 7:38 PM
Hima
Hima - avatar
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.
19th Nov 2019, 11:27 PM
Ahmed Khater
Ahmed Khater - avatar