User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Mar 2008
Posts: 19
Reputation: sjgriffiths is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
sjgriffiths sjgriffiths is offline Offline
Newbie Poster

Changing string

  #1  
Apr 25th, 2008
Hello

I was wondering whether someone could help me with the below

I have a variable called:

char company[50];

I have a string which contains:

"STEPHEN JOHNSON LTD"

I want to change the LTD to LIMITED

How do i do this?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,300
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 455
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Changing string

  #2  
Apr 25th, 2008
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:
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main ( void )
  5. {
  6. char company[50] = "STEPHEN JOHNSON LTD";
  7. char *match = strstr ( company, "LTD" );
  8.  
  9. if ( match != NULL ) {
  10. memmove ( match + 7, match + 3, strlen ( match + 3 ) );
  11. memcpy ( match, "LIMITED", 7 );
  12.  
  13. puts ( company );
  14. }
  15.  
  16. return 0;
  17. }
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Mar 2008
Posts: 19
Reputation: sjgriffiths is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
sjgriffiths sjgriffiths is offline Offline
Newbie Poster

Re: Changing string

  #3  
Apr 25th, 2008
Ha, fantastic Narue...

One more thing. I dont want to change the ltd if its not the end

eg

If i Had

Ltd Stephen Johnson Ltd

I would change this to Ltd Stephen Johnson Limited

Does that make sense?
Reply With Quote  
Join Date: Sep 2004
Posts: 6,300
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 455
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Changing string

  #4  
Apr 25th, 2008
>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:
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main ( void )
  5. {
  6. char company[50] = "LTD STEPHEN LTD JOHNSON LTD";
  7. char *match = company;
  8. char *last = NULL;
  9.  
  10. while ( ( match = strstr ( match, "LTD" ) ) != NULL ) {
  11. last = match;
  12. match += 3; /* Skip the match so we don't loop forever */
  13. }
  14.  
  15. if ( last != NULL ) {
  16. memmove ( last + 7, last + 3, strlen ( last + 3 ) );
  17. memcpy ( last, "LIMITED", 7 );
  18.  
  19. puts ( company );
  20. }
  21.  
  22. return 0;
  23. }
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:
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int main ( void )
  6. {
  7. char company[50] = "LTD STEPHEN LTD JOHNSON LTD";
  8. size_t i = strlen ( company );
  9.  
  10. do
  11. --i;
  12. while ( isspace ( company[i] ) );
  13.  
  14. if ( i >= 2 && strncmp ( &company[i - 2], "LTD", 3 ) == 0 )
  15. strcpy ( &company[i - 2], "LIMITED" );
  16.  
  17. puts ( company );
  18.  
  19. return 0;
  20. }
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 10:32 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC