•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 423,281 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 5,273 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 261 | Replies: 3
![]() |
If you know the string is big enough to hold all of the extra characters, it's a relatively simple matter of finding instances of "LTD", shifting everything after the matched substring (to make room), and copying the new substring in:
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> int main ( void ) { char company[50] = "STEPHEN JOHNSON LTD"; char *match = strstr ( company, "LTD" ); if ( match != NULL ) { memmove ( match + 7, match + 3, strlen ( match + 3 ) ); memcpy ( match, "LIMITED", 7 ); puts ( company ); } return 0; }
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
>Does that make sense?
Absolutely. Unfortunately there's not a standard strrstr function, so the most straightforward solution is to save the previous match until strstr fails. At that point you know you've got the last instance of the substring:
Of course, that might not be a perfect solution if by "not the end" you mean the last word in the string. In that case both the test and replacement are conceptually easier, especially if you don't care about trailing whitespace:
Absolutely. Unfortunately there's not a standard strrstr function, so the most straightforward solution is to save the previous match until strstr fails. At that point you know you've got the last instance of the substring:
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> int main ( void ) { char company[50] = "LTD STEPHEN LTD JOHNSON LTD"; char *match = company; char *last = NULL; while ( ( match = strstr ( match, "LTD" ) ) != NULL ) { last = match; match += 3; /* Skip the match so we don't loop forever */ } if ( last != NULL ) { memmove ( last + 7, last + 3, strlen ( last + 3 ) ); memcpy ( last, "LIMITED", 7 ); puts ( company ); } return 0; }
c Syntax (Toggle Plain Text)
#include <ctype.h> #include <stdio.h> #include <string.h> int main ( void ) { char company[50] = "LTD STEPHEN LTD JOHNSON LTD"; size_t i = strlen ( company ); do --i; while ( isspace ( company[i] ) ); if ( i >= 2 && strncmp ( &company[i - 2], "LTD", 3 ) == 0 ) strcpy ( &company[i - 2], "LIMITED" ); puts ( company ); return 0; }
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Creating a ref to a string ? (Python)
- add values to enums or change string (Java)
- Problem connecting to the database after changing the database server (ASP.NET)
- Problems with changing values returned by RegQueryValueEx (C)
- Changing ISP (Web Browsers)
- changing from onclick to onsubmit? possible? (JavaScript / DHTML / AJAX)
- Changing uppercase letter to lower case (Java)
Other Threads in the C Forum
- Previous Thread: problems with strcmp
- Next Thread: CSMA/CD



Linear Mode