Thread: Trouble inserting characters into a string

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    51

    Trouble inserting characters into a string

    Hi friends,

    I have an assignment in C where I have to insert a user inputted character into a string at the position the user states. I have the code below but it does not seem to work, let alone compile. I hate to come here for homework help, but I believe that its best learning by being taught by smart people, such as the users here! Anyways, the code I have so far is below.

    Code:
    # include <stdio.h># include <string.h>
    
    
    void insert (char origString[], char insertion_char, int position_of_insertion)
    {
        int new_length = strlen(origString) + 1;
        char new_str[new_length];
    }
    
    
    int main()
    {
        char origString[20], insertion_char[1], new_str;
        int position_of_insertion, i, new_length;
    
    
        printf("Enter a string: ");
        scanf("%s", origString);
    
    
    
    
        printf("Enter a character to insert: ");
        scanf("%s", insertion_char);
    
    
    
    
        printf("Which position should the character be inserted before: ");
        scanf("%d", &position_of_insertion);
    
    
        for (i=0; i<position_of_insertion; i++)
        {
            new_str[i]= insert(origString, insertion_char, position_of_insertion);
        }
    
    
        for (i=position_of_insertion+1; i<new_length; i++)
        {
            new_str[i] = insert(origString, insertion_char, position_of_insertion);
        }
    
    
        printf("The new string is: %s", new_str);
    
    
    
    
        return 0;
    }
    The output should be like this:
    Code:
    Enter a String:lovecats4ever
    Enter a character to insert: I
    Which position should the character be inserted before:0
    The new string is Ilovecats4ever

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Have you tried to address the various warnings and errors?

    Code:
    main.c||In function 'insert':|
    main.c|8|warning: unused variable 'new_str'|
    main.c||In function 'main':|
    main.c|36|error: subscripted value is neither array nor pointer|
    main.c|36|warning: passing argument 2 of 'insert' makes integer from pointer without a cast|
    main.c|5|note: expected 'char' but argument is of type 'char *'|
    main.c|42|error: subscripted value is neither array nor pointer|
    main.c|42|warning: passing argument 2 of 'insert' makes integer from pointer without a cast|
    main.c|5|note: expected 'char' but argument is of type 'char *'|
    main.c|46|warning: format '%s' expects type 'char *', but argument 2 has type 'int'|
    ||=== Build finished: 2 errors, 4 warnings ===|
    You should code step by step - write one thing, compile, test, and verify - add some more code to bring you one step closer to your goal - compile, test, verify - repeat.
    A development process

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Replace
    Code:
     char insertion_char[1]
    to
    Code:
     char insertion_char
    since it is just a char.

    Then of course replace this
    Code:
    printf("Enter a character to insert: ");
        scanf("%s", insertion_char);
    with this
    Code:
    printf("Enter a character to insert: ");
        scanf("%c", &insertion_char);

    Ok when a function returns nothing, then in her prototype there is as return value the keyword void.
    As a result we can not assign what the function I just described to a variable.
    If we want a function to return something, then we have to write the type of that something in the return value

    e.g
    Code:
    int foo(void)
    {
         a = 5;
         return 5;
    }
    
    int main(void)
    {
          int b;
          b = foo();
      
          return 0;
    }
    If foo had a return value of void then we would have the compiler giving as an error!

    At first make sure you understood what i said.

    Are you sure the output should be like this?
    I mean the letter in position zero is 'l' in terms of an array... For example in the same example you have, if I give position 3 what will happen?

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    If it was 3 it would be lovIecats4ever. Thats what I hoped the for loop was able to achieve.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    So if the position is zero then it should be Iovecats4ever and not Ilovecats4ever .

    Did you understand the rest I said about the code?

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    Yes I understand what you said about the code. Also no, I am inserting the character, not replacing. That is why it gets inserted into the string, instead of replacing the character.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Oh I see. Thank you. Ok so now apply what you learned from post #2 and #3 and post your updated code

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    Code:
     include <stdio.h># include <string.h>
    int insert (char origString[], char insertion_char, int position_of_insertion)
    {
        int new_length = strlen(origString) + 1;
        char new_str[new_length];
    
    
        return new_length;
    }
    
    
    int main()
    {
        char origString[20], insertion_char, new_str;
        int position_of_insertion, i, new_length;
    
    
        printf("Enter a string: ");
        scanf("%s", origString);
    
    
    
    
        printf("Enter a character to insert: ");
        scanf("%c", &insertion_char);
    
    
    
    
        printf("Which position should the character be inserted before: ");
        scanf("%d", &position_of_insertion);
    
    
        for (i=0; i<position_of_insertion; i++)
        {
            new_str[i]= insert(origString, insertion_char, position_of_insertion);
        }
    
    
        for (i=position_of_insertion+1; i<new_length; i++)
        {
            new_str[i] = insert(origString, insertion_char, position_of_insertion);
        }
    
    
        printf("The new string is: %s", new_str);
    
    
    
    
        return 0;
    }
    I believe this is what you meant? Thanks for your help so far mate. I am getting 2 errors:

    Code:
    LINE 39 error: subscripted value is neither array nor pointer
    LINE 44 error: subscripted value is neither array nor pointer

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Tell us what you think that this line does?

    Code:
    new_str[i]= insert(origString, insertion_char, position_of_insertion);
    Fact - Beethoven wrote his first symphony in C

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    And one more - What this one does

    Code:
    printf("The new string is: %s", new_str);
    Fact - Beethoven wrote his first symphony in C

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    It runs the loop, stops at the user entered position point, and inserts the insertion_character. At least that was my intentions for both of them.

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    The value of new_str[i] becomes what the function "insert" is returning.

    And the function "insert" does not change the size of any character arrays. Instead it creates a new array of size origString + 1 and then once the function has ended, it no longer exists. So if "hello" was in origString, the function would return 6. Also note that origString array was already strlen + 1 -> It has a '\0' character on the end.
    Fact - Beethoven wrote his first symphony in C

  13. #13
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    new_str is also declared as a char -> Not a char* which you can malloc memory to and not an array.
    Fact - Beethoven wrote his first symphony in C

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    Thanks guys for pointing me in the right direction. I figured out the problem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a string
    By juice in forum C Programming
    Replies: 12
    Last Post: 12-12-2011, 11:53 PM
  2. inserting characters mid-stream?
    By spongefreddie in forum C Programming
    Replies: 1
    Last Post: 10-15-2010, 02:42 AM
  3. Inserting hex characters in the output
    By earth_angel in forum C++ Programming
    Replies: 5
    Last Post: 06-29-2005, 11:52 AM
  4. Replies: 0
    Last Post: 04-05-2003, 09:33 AM
  5. inserting characters into a binary tree
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-06-2001, 04:08 PM

Tags for this Thread