Up vote 1 down vote favorite share g+ share fb share tw.
Here is my program which reverses a string. I am trying to use function strrchr at the end because I am entering the string James Bond on standard input. So I want to see if strrchr function gives a pointer to value character B of the above string.
#include #include int main () { char rt100; char temp,*op; printf("enter a string\n"); scanf("%s",rt); printf("the string you entered is %s\n",rt); int i, k, length; i=0; k=strlen(rt); printf("strlen gives =%d\n",k); length=k; length--; for(;i Is above use of strrchr wrong? C string link|improve this question edited Jun 22 '11 at 6:39X-Istence4,1401428 asked Jun 22 '11 at 6:35Registered User726110 90% accept rate.
Try this: #include #include int main () { char rt100; char temp,*op; printf("enter a string\n"); fgets(rt, 100, stdin); printf("the string you entered is %s\n",rt); int i,k,length; i=0;k=strlen(rt); printf("strlen gives =%d\n",k); length=k; length--; for(;i For more info regarding scanf, gets and fgets, see this.
I really like this answer because scanf made the hair on my neck stand up. I'd upvote this if only the evils of scanf were mentioned in clarification :). – jpm Jun 22 '11 at 6:49 @jpm Good suggestion.
I have added link to another question. Rest is left as an exercise for the reader (or OP). :) – Rumple Stiltskin Jun 22 '11 at 6:55 Stitlskin I read the man page of fgets after your code but I want to understand how does fgets by passes that space thing.i.e.
If space is present then how does fgets handles it which I am not able to handle with scanf? – Registered User Jun 22 '11 at 7:28.
Before dereferencing op with the * unary operator, you have to check whether it's NULL (this will be the case when the character is not found): op = strrchr(rt, 'B'); if (op! = NULL) printf("strrchr gives %c\n", *op).
And the reason it's not found is because scanf("%s", rt); stops taking input at the delimiter (space) and "James" doesn't contain 'B'. It's really a moot point, though, because as @Rumple demonstrated, we shouldn't even be using scanf, since it's just begging for stack overflow attacks. Use fgets instead.
– jpm Jun 22 '11 at 6:47 @jpm I want to know how does fgets works i.e. What does fgets does so that the space present in the string is also stored and why is my program not able to do that. – Registered User Jun 22 '11 at 7:30 Both scanf and fgets are using IO primitives internally.
The difference is that scanf is defined to stop at delimiters, where as fgets is designed to try read a specified number of bytes, only stopping if it's interrupted or reached end-of-file. You could say that the reason fgets doesn't break on delimiters is that it's not using scanf internally. – jpm Jun 23 '11 at 7:34.
From the fine manual: Upon successful completion, strrchr() shall return a pointer to the byte or a null pointer if c does not occur in the string. Since there is no 'B' in rt when you call strrchr(rt, 'B'), you get a NULL pointer back and then you try dereference it in your printf call. Hence your segfault.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.