tags:

views:

78

answers:

3

Hey all,

I'm trying very hard to figure out a way to parse a string and "highlight" the search term in the result by making it uppercase.

I've tried using strstr and moving a pointer along and "toupper"ing the characters, to no avail.

char * highlight( char *str, char *searchstr ) {

 char *pnt=str;
 int i;

 pnt=strstr(str,searchstr);

 while(pnt){

 printf("ststr retured: %s\n", pnt);

 for(i=0;i<strlen(searchstr);i++) {
  printf("%c",toupper(pnt[i]));
 }
 printf("\n"); 
 pnt=pnt+strlen(searchstr);
 pnt=strstr(pnt,searchstr);

}

 return str;

}

Any advice is greatly appreciated.

+1  A: 

You appreciate that the function takes the string as an argument and then returns that same string, while having -not- modified that string? all the function does is print to stdout the capital characters.

At some point, you would need to change the string itself, e.g.;

pnt[i] = toupper( pnt[i] );

Blank Xavier
+2  A: 

Since Schot mentioned every occurrence:

#include <string.h>

char *highlight(char *str, char *searchstr) {

  char *pnt = str;

  while (pnt = strstr(pnt, searchstr)) {
    char *tmp = searchstr;
    while(*(tmp++)) { *pnt = toupper(*pnt); pnt++; }
  }
  return str;

}

int main() {
  char s[] = "hello world follow llollo";
  char search[] = "llo";
  puts(highlight(s, search));
  return 0;
}

output is:

$ ./a.out 
heLLO world foLLOw LLOLLO
動靜能量
true... seems like the OP is finding a way to make it first working for the simplest case...
動靜能量
I'm looking to highlight every instance of the searchstr.
Nick
ok, here is the new version
動靜能量
Lovely! My problem now is being able to do this with strings declared as char *string. I'm a total noob so I'm not sure what the difference is between char string[] and char *string. When I try this with *string it segfaults-- is this because *strings are not null terminated??
Nick
A: 

Like Blank Xavier said, you probably want to modify the actual string. toupper does not change the value of the character you supply, but returns a new character that is its uppercase version. You have to explicitly assign it back to the original string.

Some additional tips:

  • Never do multiple strlen calls on a string that doesn't change, do it once and store the result.
  • You can express the promise of not changing searchstr by declaring it as const char *.
  • Below is an example with a (in my opinion) easy method of looping through all strstr matches:
#include <string.h>
#include <ctype.h>

char *highlight(char *s, const char *t)
{
    char *p;
    size_t i, len = strlen(t);

    for (p = s; (p = strstr(p, t)); p += len)
        for (i = 0; i < len; i++)
            p[i] = toupper(p[i]);

    return s;
}
schot
Hey thanks! Unfortunately, I'm getting this error when compiling this code: test.c:638: error: 'for' loop initial declaration used outside C99 mode. Is there any way to break up that for loop?
Nick
@Nick, I wrote C99 for brevity. See the updated version.
schot
You guys are the best, thanks heaps!
Nick