Change a string in a function that recives a pointer to that string

This is a discussion on Change a string in a function that recives a pointer to that string within the C Programming forums, part of the General Programming Boards category; Hello there, I'm new here and I need some help: I am asked to write a function that recieves a ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    9

    Change a string in a function that recives a pointer to that string

    Hello there, I'm new here and I need some help:
    I am asked to write a function that recieves a pointer to string, a zero-based integers, "start" and "end", and int "dist".
    In this function I need to modify the string by moving the substring starting from "start" to "end" by "dist" characters. for example: the string "ronald mashu" if I call the function move(s, 0, 3, 2) when s is the pointer to "ronald mashu", if I print s after the function call I should recieve "ldrona mashu".
    The problem is that inside the function I manage to get the correct output in a local pointer but I can't seem to get the original pointer point to it.
    Here's the code:
    Code:
     
          1 #include <stdio.h>
          2 #include <stdlib.h>
          3 #include <ctype.h>
          4 #include <string.h>
          5
          6 int move_chars (char *str, int start, int end, int dist) {
          7    int index, subindex = 0;
          8    char *sub = malloc ((end - start + 1) * sizeof(char));
          9    char *old = malloc ((start + 1) * sizeof(char));
         10    char *subStart = sub;
         11    char *oldStart = old;
         12    /* Validity Checks */
         13    if (start < 0 || end < 0 || dist < 0) {
         14       printf("Invalid Arguments");
         15       return 1;
         16    }
         17    for (index = 0 ; index < start ; index++) {
         18       old[0] = str[0];
         19       str++;
         20       old++;
         21    }     22    for (index = start ; index <= end ; index++) {
         23       sub[0] = str[0];
         24       subindex++;
         25       str++;
         26       sub++;
         27    }
         28    for (index = 0 ; index < dist ; index++) {
         29       old[0] = str[0];
         30       str++;
         31       old++;
         32    }
         33    sub = subStart;
         34    old = oldStart;
         35    strcat(old, sub);
         36    strcat(old, str);
         37    str = old;
         38    free(old);
         39    free(sub);
         40    return 0;
         41 }
         42
         43 int main() {
         44    char *s = "ronald mashu";
         45    move_chars(s, 0, 3, 2);
         46    printf("%s\n", s);
         47    return 0;
         48 }
    Ideas?
    Thanks in advance

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    You can't change where "str" points and have it be reflected in main(). You have to modify the string through the pointer only.

    First thing, though: given your description, I would expect "ronald mashu" to be changed to "rorona mashu". Your example includes rotation, which your function description does not mention.

    With that assumption in mind, a simple hard-coded function for this would be:
    Code:
    void move(char *s)
    {
      s[5] = s[3];
      s[4] = s[2];
      s[3] = s[1];
      s[2] = s[0];
    }
    Note how s is written to in-place. This is how your function should operate, but of course you should use loops to make it general. Keep in mind that the loop will look different for copying to the left vs copying to the right.

    Finally, you should not pass a string literal to the function, because you should not be modifying string literals. Instead, use:
    Code:
    char s[] = "ronald mashu";

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    First, read cas's post.

    Don't include line numbers in your code posts.

    If you're having move_chars return an error code, then don't ignore it in main.

    In order to set main's s contents to what old is pointing to, you'll want to use strcpy. However, another problem here is that you've modified where str points with all the str++ statements. You'll have to save a pointer to the start of the input string to use in strcpy.

    There are other errors/oddities in your code, but that's a start.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    9
    Your guideness helped alot! Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to function with string
    By WSUstudent in forum C Programming
    Replies: 1
    Last Post: 10-10-2011, 06:37 PM
  2. string pointer passed to a function help
    By BrandNew in forum C Programming
    Replies: 7
    Last Post: 03-07-2011, 10:08 AM
  3. string(pointer to function)
    By Darkinyuasha1 in forum C Programming
    Replies: 11
    Last Post: 11-30-2006, 01:07 PM
  4. Passing string along function. Pointer.
    By Cheeze-It in forum C++ Programming
    Replies: 4
    Last Post: 06-23-2002, 07:23 PM
  5. how to change and return a string from function?
    By cywong18 in forum C Programming
    Replies: 8
    Last Post: 03-23-2002, 07:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21