Thread: appending a charecter n the middle of a string

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    35

    appending a charecter n the middle of a string

    okay so lets say I have a string that says this:

    sam plays basketball all day

    i want to append another space charecter after the word basketball
    so it would be sam plays basketball all day

    now there are two spaces after the word basketball, how would I
    be able to do that? since the strcat funcctions only append at the end of a string and not in the middle

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you had 3X5 cards with letters on them in front of you, and those letters, when laid out next to each other showed the message "Sam plays basketball all day long", how would you add an extra space after basketball?

    You program it the same way that you would do it except - you can't "push" any of the cards along the tabletop or desktop. You have to pick them up and place them.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    35
    so what your saying is this cant be done? like i was thinking of having a ptr pointing to that string and incrementing the pointer until its at the end of the basketball and add a space there, but that didnt work :/, im a noob at c :/ haha

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Absolutely it can be done, and it's done the same way you'd do it with cards of letters, on your tabletop.

    You can't "add" a space. That's an element in the array, and it can't be moved -- but the CONTENTS of the element CAN BE MOVED.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    35
    well basically i want to create a find and replace command in c, this is the following problem I run into so basically i ask the user to type in a string: say the string is: sam plays basketball all day, then i ask the user to type in the word they want to replace in that string, i use ststr to find the occurence of that string so lets say the user types in sam, then i ask the user to type in the word that will be used to replace that word and so say the user types in jeremy, now the string will say Jeremyays basketball all day, im using strncat, im running into that overlapping problem, instead i want it to say, jeremy plays basketball all day

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Nope! Of course, I know exactly what you want to do - but I'm going to fire up your problem solving a bit.

    Quit guessing around at the keyboard, and get out some pieces of paper and put each piece of paper with a letter on it, so the message is "Sam plays basketball all day long".

    Now, without sliding any letters, replace Sam in that bunch of letters, with Jeremy.

    And don't TOUCH the keyboard, until you do it, and see the pattern you followed - it's all right there.

    Hollywood Quote:
    "The killer part is, you already know!" -- Jacob to Bella in New Moon

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    35
    haha okay im gonna try that haha just realized you replied now

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    35
    so shifting the contents over would have to be done?

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yep!

    The array has to be sized with enough room for the extra letters (or enlarged). And the shifts have to be from one end, also. Which one?

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    35
    For the shifting, lets say I have a string that says Jennifer Aniston is so attractive, now i wanna replace the word "so" with extremly, now for the shifting when I shift, I will only have to shift everything after extremly right?! how would I do that?!

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Oh, I believe the eggs will have to tell you. The letters on paper were not talkative enough, so it's up to the eggs.

    Get a carton of eggs, and carefully remove the right most half of the eggs - leaving 6 eggs in the carton. Now take four of the eggs you removed from the carton, and carefully insert them into the middle of the other eggs. They will be the 4th and 5th eggs from the left, in both rows in the carton.

    Now, how did you do that?

    NO, you can't just put the eggs on the end of the rows - this is SCIENCE, dammit!

    Don't ask humans anymore - listen to the eggs. Eggs are ancient and wise things. They will talk to you with Good Medicine.

    Don't leave them out of the refrigerator, too long.

  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    Another way to look at it is look at your keyboard, if it it is standard qwerty format this will be easy.

    Lets say you want to add another letter (any letter) to the keyboard row of "asdfghjkl" you cannot "grow" that row but if you look above it to the "qwertyuiop" row that has one more space to add the other letter and hold all letters from the other row.

    Strings are stored like arrays. Think of each row as an array.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Change the problem.
    Don't try and modify the existing string, instead build a new string. Loop over the input string and when you hit the start of your special word, copy the replacement word into the new string instead and skip to the end of special word in the original string. Or else if it's any other character, just copy those characters over.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Here's a quick example, it's not the best code but w/e..

    Code:
    char *mod = "sam plays basketball all day";
    	int median = strlen("basketball");
    	
    	char *tmp = strstr(mod, "basketball");
    	int pos = abs((int)(mod-tmp)) + median;
    	
    	char *ret = malloc(100);
    	memcpy(ret, mod, pos);
    	ret[pos] = 0x20;				/* hexcode for space */
    	memcpy(ret + pos + 1, tmp + median, strlen(tmp));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 20
    Last Post: 03-08-2011, 05:16 PM
  2. replacing a charecter in a string
    By Dontgiveup in forum C Programming
    Replies: 10
    Last Post: 06-03-2009, 11:54 PM
  3. Inserting a character in the middle of a string
    By winsonlee in forum C Programming
    Replies: 1
    Last Post: 03-20-2004, 04:35 AM
  4. String length from the middle?
    By mart_man00 in forum C Programming
    Replies: 18
    Last Post: 03-11-2003, 03:54 AM
  5. String appending
    By larry in forum C++ Programming
    Replies: 9
    Last Post: 09-30-2001, 04:34 AM