Reputation: 47
This function is to find out the last occurance of a char in a string. eg.
Enter a string: abcdefdfdfghh
Enter the target char in the string: f
Resultant string = fghh
I know there is something terribly wrong with my pointers. Can someone help me correct it?
char *strrchr2(char *s, char chr);
int main()
{
printf("Enter a string value: \n");
gets_s(s, sizeof(s));
printf("Enter the character: \n");
scanf_s("%c", &chr, 1);
printf("Resultant string is: %s\n",*(strrchr2(s, chr)));
_getch();
return 0;
}
char *strrchr2(char *s, char ch1)
{
char ns_q4[150];
char *ns_ptr;
int count = 0;
char ans;
int i = 0;
ns_ptr = ns_q4;
while (*(s + i) != '\0')
{
count++;
i++;
}
s = &s[0];
/*iterates from back,breaks when match found and from there iterates newly till end*/
for (int j = count; j >= 0; j--)
{
if (*(s + j) = ch1)
{
for (int k = j; k < count; k++)
{
*ns_ptr = s[k];
ns_ptr++;
}
break; }
}
ns_ptr = ns_q4;
ans = *ns_ptr;
return ans;
}
Upvotes: 0
Views: 208
Reputation: 64682
I'm not even going to try to read your code.
Instead:
char *strrchr2(char *s, char ch1)
{
char* r; // Return value set to NULL, in case ch1 is not found.
for(r=NULL; s != '\0'; ++s)
{
r = (ch1 == *s)? s : r; // If the character is found...
}
return r;
}
If you don't like the ternary form:
char *strrchr2(char *s, char ch1)
{
char* r; // Return value set to NULL, in case ch1 is not found.
for(r=NULL; s != '\0'; ++s)
{
if (ch1 == *s)
{
r = s; //If the character is found...
}
}
return r;
}
In a follow up, poster writes:
"But the error is showing at the statement in the main func-->> printf("Resultant string is: %s\n",*(strrchr2(s, chr)));. How can I correct it?"
%s
format expects a char*
. The function returns a char*
. You have everything you need! Why did you add an extra *( )
around the function??? Why??Upvotes: 1
Reputation: 4612
There's no need for more than one loop here. All you need to do to find the last occurrence of ch1
in s
is this:
char *last = NULL;
while (*s != '\0') {
if (*s == ch1)
last = s;
++s;
}
return last;
Update: Attempting to be more efficient by iterating from the end of the string is pointless, since you first have to find the end of the string first. Whether you do it with an extra loop, or if you call strlen()
, the result is that you are iterating the entire length of the string just to find the end, and then iterating some additional amount backwards to find the last occurrence. My method iterates the entire string, once and only once. The only way it is more efficient to start from the end of the string is if you already know where the end is in advance, and you can pass that information in to strrchr2()
as an additional parameter so that strrchr2()
doesn't have to waste time trying to find the end again every time it is called.
Upvotes: 2
Reputation: 11629
Use ==
instead of =
replace if (*(s + j) = ch1)
with if (*(s + j) == ch1)
also, null teminate it when you are done:
ns_ptr++;
*ns_ptr = '\0';
and this is not required:
s = &s[0]
you are not changing what s
points to
Upvotes: 3