Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8674185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:38:46+00:00 2026-06-12T19:38:46+00:00

First time posting here so I’m sorry if I mess up. I need to

  • 0

First time posting here so I’m sorry if I mess up.
I need to search a string and return any strings containing the search data with the search data highlighted.

Example:
If my initial string is: Hi my name is, and my search term is: name, then the output should be: Hi my NAME is

This is a quick code I wrote that works but it only works once. If I try and search again it seg faults.

I was hoping someone could hint me at a better way to write this because this code is disgusting!

void search(char * srcStr, int n){
    int cnt = 0, pnt,i = 0; 
    char tmpText[500];
    char tmpName[500];
    char *ptr, *ptr2, *ptrLast;
    int num;


    while(*(node->text+cnt) != '\0'){ //finds length of string
        cnt++;
    }
    for(pnt = 0; pnt < cnt; pnt++){ //copies node->text into a tmp string
        tmpText[pnt] = *(node->text+pnt);
    }
    tmpText[pnt+1] = '\0';


        //prints up to first occurrence of srcStr
      ptr = strcasestr(tmpText, srcStr); 
        for(num = 0; num < ptr-tmpText; num++){
            printf("%c",tmpText[num]);
        }

      //prints first occurrence of srcStr in capitals
        for(num = 0; num < n; num++){
            printf("%c",toupper(tmpText[ptr-tmpText+num]));
      }

      ptr2 = strcasestr((ptr+n),srcStr);
        for(num = (ptr-tmpText+n); num < (ptr2-tmpText); num++){
            printf("%c",tmpText[num]);
        } 

        while((ptr = strcasestr((ptr+n), srcStr)) != NULL){
            ptr2 = strcasestr((ptr+n),srcStr);
        for(num = (ptr-tmpText+n); num < (ptr2-tmpText); num++){
            printf("%c",tmpText[num]);
        } 
        for(num = 0; num < n; num++){
                printf("%c",toupper(tmpText[ptr-tmpText+num]));
          }
          ptrLast = ptr;
      }
      //prints remaining string after last occurrence
        for(num = (ptrLast-tmpText+n); num < cnt; num++){ 
            printf("%c",tmpText[num]);
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:38:47+00:00Added an answer on June 12, 2026 at 7:38 pm

    Only because I’m stupid lazy and there were no pre-requisites for conformance with multi-byte characters (which makes this a helluva lot harder). There are more efficient ways, but barring a platform with strupr() and strlwr() it is hard to get much simpler.

    Note: this takes into account the possibility of the search txt being present in the source in mixed case rather than strictly lower case. If the problem domain is restricted to only substitute exact lower-case matches, the source dup can be dropped as well, thereby making this about a half-dozen fewer lines still.

    // search for lower case
    void substlwr(char* text, const char* str)
    {
        // dup strings for lower case  text and search content, 
        //  and upper case replacement text.
        size_t slen = strlen(str);
        char *lbase = strdup(text);
        char *lstr = strdup(str);
        char *ustr = strdup(str);
        char *found = lbase;
        char *p = NULL;
    
        // convert lowers and uppers
        for (p=lbase; p && *p; *p++ = tolower(*p));
        for (p=lstr; p && *p; *p++ = tolower(*p));
        for (p=ustr; p && *p; *p++ = toupper(*p));
    
        // replace all lowers with uppers
        while ((found = strstr(found, lstr)))
        {
            memcpy(text + (found-lbase), ustr, slen);
            found += slen;
        };
    
        free(lbase);
        free(lstr);
        free(ustr);
    }
    

    Sample invoke looks like:

    int main(int argc, char *argv[])
    {
        char text[] = "Hi! My Name is George.";
        substlwr(text, "name");
        printf("%s\n", text);
        return EXIT_SUCCESS;
    }
    

    and output looks like this:

    Hi! My NAME is George.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

first time posting here, but god know's I use this site to search for
I'm a newbie programmer posting here for the first time. Any suggestions or advice
first time posting here so be gentle :) On my web pages I have
first time posting here. I'm using a loop to create 3 buttons, but my
this is my first time posting here, I have a question which I have
first time posting in StackOverflow. :D I need my software to add a couple
this is my first time posting on here and have read alot of helpful
this is my first time posting here, but I didn't know where else to
This is my first time posting a question here, it has been a valuable
First and foremost, apologies for any cross-posting. Hope I'm not repeating an issue here,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.