Thread: How to shift strings down in an array

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    19

    How to shift strings down in an array

    I am having this problem were the user types in a substring to search in the main string and replace it with another string

    for example:

    string: hello
    sub string: el
    replace: i

    should print this: hillo

    I tried using some string functions but I am having problems with that. If anyone has a good way to approach this please share.
    Thank you!
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Posting what you've attempted would be helpful to be helpful.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    19
    this is what I have:
    Code:
    for(i = (loc - theString); i < strlen(theString); i++)
    {
    for(j = 0; j < strlen(searchFor); j++)
    {
    printf("\nthe start:%c\n", *(theString + i));
    if((counter) < strlen(replace))
    *(theString + i) = *(replace + j);
    if((counter <= strlen(searchFor) && counter >= 
    strlen(replace)))
    {
    differenceMover = strlen(searchFor) - strlen
    (replace);
    printf("%c\n", *(theString + i));
    
    if(*(theString + i - differenceMover) == '@')
    {
    *(theString + i - differenceMover) = *
    (theString + i);
    *(theString + i) = '@';
    }
    else
    *(theString + i) = *(theString + i + 
    differenceMover);
    if(i + differenceMover < strlen(theString))
    *(theString + i + differenceMover) = '@';
    
    }
    if(counter >= strlen(searchFor))
    {
    printf("end of loop\n");
    i = strlen(theString);
    }
    else
    {
    i++;
    
    
    counter++;
    
    }
    }
    }
    This is the output I am getting:
    string: hello
    search: ll
    replace: i
    hilio@

  4. #4
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Can you post all the code as there are many variables undeclared and no main?

    Also: Next time you post code, make sure it is formatted or the big names who can really help won't bother.

  5. #5
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    You seem to be over-complicating things by dereferencing all the characters using pointers and such. By referring to the strings as arrays, it becomes much more natural due to the use of indexes.
    Here's a link to some source code that might help give you somewhere to start ( I would recommend starting over ).

    Edit: Forgot the link!! C Replace String
    Last edited by jwroblewski44; 02-24-2013 at 02:08 AM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Edit: I just noticed that your initial post had the wrong output as the stated goal. If "el" is to be removed, the ending string wouldn't be "hillo", but "hilo", since only one el would remain.

    Looking at the problem:

    string: hello
    sub string: el
    replace: i

    should print this: hillo <==== should be "hilo"
    The "normal" way to locate a sub string in a string, is to use strstr() (so include string.h of course). strstr() will find the target string, and return an address to the first char of the target string - so 'e''s address will be returned.

    Now it all becomes clearer:

    1) use a char *pchr to "catch" the returned address of strstr().

    2) move the "tail" end (the part of the string to the right side of the *pchr, into a temp char array, for safe keeping. In this example it would be the "ello", on the end of hello, that would be moved.

    3) starting at pchr, copy your replacement char(s), into the string, and put an end of string char on the end of it: '\0'.

    4) strcat() the part you moved, back to the end of the string, starting at the address *after* the strlen() of the removal array.

    The thing with this kind of a problem, is you have to print out the strings you're working with, in the code, and then add the getchar() so you can see what's going on, EXACTLY. Using a debugger is even better, and watching the values as you step through the program.

    You have the right idea, but you got caught up in the complications. The above is probably also not perfect, but think of a string - your program is going to snip it in two, add a segment of string, and remove an adjacent segment of string, and then rejoin the parts in order.

    Try to keep everything as simple as you can, so you can visualize just what the program is doing, easier.
    Last edited by Adak; 02-24-2013 at 02:50 AM.

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    19
    I kind of fixed the issue buy deleting all that and using one for loop.

    Sorry that I did not provide the full code, this was a small portion of my program and did not want to put it all here.

    Also is it better to use string functions or if statments and for loops? I thought it did not matter but not sure if one was faster to run then the other.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No worries - and welcome to the forum, plot!

    For a few explicit char's - hard to beat just doing it yourself. For a large scale move, it's hard to beat the standard library, and of course, that minimizes the chances of an error in your code, as well as making it shorter and easier for others to understand, as well.

    I just happened to do this exercise, and then when the link to the program from India was posted, I checked it out - lots of similarities, but the Indian code used sprinf() which I didn't, and gained a bit more clarity, by doing so.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. logical shift and arithmetic shift.(bit shifting in C )
    By A34Chris in forum C Programming
    Replies: 20
    Last Post: 09-03-2012, 11:22 AM
  2. Shift right for an array .
    By amir1986 in forum C Programming
    Replies: 4
    Last Post: 01-22-2011, 06:56 AM
  3. how can I shift bit right a byte array
    By lovesunset21 in forum C Programming
    Replies: 10
    Last Post: 11-03-2010, 02:31 PM
  4. How to shift elements of an array?
    By zeebo17 in forum C Programming
    Replies: 25
    Last Post: 06-09-2010, 07:44 PM
  5. array shift problem
    By splendid bob in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2002, 10:11 PM