Thread: Replacing string with another string with user-inputted position

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    39

    Replacing string with another string with user-inputted position

    Hello, I know the title doesn't make much sense, so I will post the instructions that I was given below.

    I've wrote a VERY general code so far, I'm just not sure how to use a function to pass the parameters to replace it. I understand what they want, I guess I just don't understand HOW.

    Replacing string with another string with user-inputted position-hwk9-png


    Here's what I've got for code so far, if this is how a function works?

    Code:
    stringInput();
    
    
    main void()
    {
    	char inp_string, insert_string;
    	int pos;
    	
    	
    	printf("Please enter an input string");
    	scanf("%s", &inp_string);
    	
    	printf("Please enter a position");
    	scanf("%d", &pos);
    
    
    	printf("Please enter an insert string");
    	scanf("%s", &insert_string);
    }
    
    
    
    
    stringInput()
    {
    	
    	
    	
    	
    	
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Why don't you study how the likes of strcpy and strcat work, then start to base your ideas on how to solve the problem based on that knowledge.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Well the first thing you will need is some arrays for your strings, don't forget to make them large enough to hold your desired strings.

    Then you will need to insure that the user doesn't try to enter a string larger than your array using the correct width specifier for your scanf() calls.


    if this is how a function works?
    You may want to re-read your instructions, pay closer attention to "c.".

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    C:
    char string[10000000000000]; or whatever length you want it. then treat it as an array.

    Visual aids.

    Get some shoe boxes. Line them up. put numbers on them 0 thru x amount. That is your first string.

    Now get a few more shoe boxes, number them 0 thru x amount. that is your second string. Now pick a number. Then take the second set of shoe boxes and figure out how to insert them into your first string of shoe boxes using a zero based number system without over running the first set of shoe boxes.

    you should see some complications if your second set of shoe boxes are too long to put them into the first set. now practice error checking.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    Check what the string-functions like strcpy() , strcat(), strlen() etc do.
    Use char-pointers and do not forget that your program can crash when
    you do not keep track of your pointers and when you write/copy data
    to undeclared memory.

    For example:

    char orig[80], tmp[80], *p1;

    For example orig[] contains the string "HELLO WORLD" and you want to insert something on the position of the space between HELLO and WORLD,
    as you can see that space is located in orig[5]

    You let char-pointer p1 point to the position of the space between HELLO and WORLD as follows:
    p1 = orig+5;

    You can now strcpy() values from p1 into tmp[] or strcpy() values into orig[] by using p1

    Note: Your program will crash if orig[] or tmp[] are to small to contain the required data, also crashes if p1 does not point to the right position.

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    If you are still around trying to figure this out.
    Code:
    temp char size of x+y+1
    strncpy
    strcat
    strcpy
    malloc
    is what I used ( more than once with some ) to figure this out. strncpy is a good helper. If you're still working on this, I'd suggest looking into that function.

  7. #7
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    Quote Originally Posted by userxbw View Post
    If you are still around trying to figure this out.
    Code:
    temp char size of x+y+1
    strncpy
    strcat
    strcpy
    malloc
    is what I used ( more than once with some ) to figure this out. strncpy is a good helper. If you're still working on this, I'd suggest looking into that function.
    For anyone still willing to help, I've been working on it a little more and researched some of the string functions, not sure if I did it correctly, but I wrote pseudocode.

    For the parts where I don't know what to put for C logic, I just put pseudocode and I tried to comment out my reasoning.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    stringInput();
    
    
    main void()
    {
    	
    	int pos; tracker1, tracker2,
    	char originalString[100], temporaryString[100]; blank[100];
    	
    	
    	
    	
    	printf("Please enter an input string");
    	scanf("%s", &originalString[0]);
    	
    	printf("Please enter a position");
    	scanf("%d", &pos);
    
    
    	printf("Please enter an insert string");
    	scanf("%s", &temporaryString[0]);
    }
    
    
    
    
    char[] stringInput(char originalString[100], temporaryString[100], blank[100], int pos)
    {
    	
    int arrayoneTracker = 0;
    int arraytwoTracker = 0;
    int arraythreeTracker = 0;
    
    
    int length = strlen(temporaryString);
    printf("
    	
    for(int i = 0; i < pos; i++)  
    {					
    
    
    blank[i] = originalString[i];	//third aray after this should be "welcome to my "
                                    //insert each letter into the third array
    }
    
    
    for(int i = 0; i < length; i++)    
    {										//third array after this should be "welcome to my CPRE "
    		blank[i] = temporaryString[i];		
    }										//add each letter in the third array
    
    
    while(first array tracker < first array length)
    {
    
    
    
    
    }												//third array after this should be "welcome to my CPRE class"
    												//add the rest of the array to the third array
    	
    	
    	return blank[];
    	
    	
    }

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I took three chars
    Code:
    #define MAX_LEN 256
    st1[MAX_LEN];
    st2{MAX_LEN]
    st3{MAX_LEN]
    In main, using fgets(string1, MAX_LEN, stdin) to get both start string, and substring. then scanf to get position to insert
    chars are int as well %c to get letter in printf so you can (try to) see what is going on while doing this.
    Code:
    for ( i = pos; i < totalSize; i++)
    {
    
        a = st3[i];
       printf("a = %c\n\n", a);
    
    
         if statment
         {
               // code
        }
       //code
    }
    loop copied srt1 ( main string) into st3 holding string
    got total length using strlen for both main and substrings put that into a int var.
    use st3 and keep track of where it is at in second loop. while within that second loop using an if statement ,you almost got it.

    you need to keep a temp of main string, then over write the main string starting at insert position to end of substring,

    then use that temp string to pick up where the substring left off at to complete the copy of what is left of the main string using the temp string back into that main string.

    using an if statement inside of loop to copy substring into main string to check using size of subsrting against a starting point, which usually is 0. but not always. while using that and it looks like you know how to keep increment counts in loops to stop the loop and do the dirty work too.

    sounds complicated, it kind of is, but not really when you get it figured out in your head then work out on paper. and use printf to see what is going on while coding compiling running checking then doing it again if needed. making needed adjustments along the way.

    then when you get it figured out, shove it in a function, then make sure it still works.
    I cannot draw array charts in this ...
    Last edited by userxbw; 11-03-2017 at 12:19 PM.

  9. #9
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    Code:
    int pos; tracker1, tracker2,
    line 11. This doesn't make sense. I believe you only need one int variable anyway, so you should get rid of tracker1, tracker2.
    Code:
    char originalString[100], temporaryString[100]; blank[100];
    line 12. Only two strings are needed here. You need the original and a temporary.
    Code:
    scanf("%s", &originalString[0]);
    scanf("%s", &temporaryString[0]);
    line 18 and 25. This is fine, but it might be redundant to use an address operator (&) as well as an index. They kind of counterbalance eachother. One cancels the other. A string name is converted into a pointer when it is passed into a function such as scanf, so while scanf asks for an address rather than a value, a string name provides that address. It works differently for integer variables because those hold values rather than addresses.

    You haven't called stringInput() in main.

    stringInput's function delcaration is missing types in its parameter list, and it lists blank[100] as a parameter which has not been given a value. Additionally, I think it would be preferable to create blank within the function as a temporary string which will go out of scope when the function returns. The modified string would then be the original string, but blank would be a copy of it while you still need a copy. The way I see the problem, you don't need the original strings once you have the final result.

    As I was writing this though, I realized you don't even need a copy if you prepare the original string first by moving the end portion of the string to where it needs to be before you insert the middle portion. You can pick what you want to do.

    Code:
    int arrayoneTracker = 0;
    int arraytwoTracker = 0;
    int arraythreeTracker = 0;
    line 34. I believe all the information you need should be contained in originalString, temporaryString, and pos. I don't believe these tracker variables are needed.
    Code:
    printf("
    line 40. unfinished printf
    Code:
    for(int i = 0; i < pos; i++)
    {
    blank[i] = originalString[i];
    }
    line 42. I think this is okay, but there is a more concise way. To assign one string to another from its beginning, you can use char* strcpy(char* s1, char* s2).
    Code:
    for(int i = 0; i < length; i++)
    {
    blank[i] = temporaryString[i];  
    }
    line 51. I don't believe you want i to begin at 0. Don't you want it to begin at pos? Then it should also end at pos + length rather than just length. Again though, strcpy would be better. You just have to do something like this: strcpy(&originalString[pos], temporaryString). The address operator and the index will cancel eachother out and you will be writing at pos rather than at the 0 index.
    Code:
    return blank[];
    line 67. Arrays get converted into pointers when passed into a function, which means the values persist and a return is not necessary. The function should probably just have a void return type.
    Last edited by jack jordan; 11-03-2017 at 12:16 PM.

  10. #10
    Registered User
    Join Date
    Nov 2017
    Posts
    6
    String.replace() will replace every ocurrance in your input String:
    String userInput = input.nextLine();
    String replaced = userInput.replace("hate","love");[COLOR=#858C93][FONT=inherit]// Here you have your new string
    Last edited by Salem; 11-12-2017 at 02:13 AM. Reason: spammy links deleted

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read file line, search for string, get string position
    By taifun in forum C++ Programming
    Replies: 15
    Last Post: 03-24-2014, 02:55 PM
  2. Replies: 7
    Last Post: 03-17-2012, 09:36 PM
  3. Single line user inputted string to Array?
    By Sparrowhawk in forum C Programming
    Replies: 11
    Last Post: 12-14-2008, 08:10 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Replies: 0
    Last Post: 04-05-2003, 09:33 AM

Tags for this Thread