Thread: replace function

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    5

    replace function

    How do you use the replace function to change a string.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    5
    Thanks for the correction. My question relates to writing a function that copies the contents of a string and then replaces it in reverse order. I am not fully aware of all the functions in C, so based on what I was able to find out, I thought there might be one.
    Here is my code:
    Any help would be appreciated!

    #include <stdio.h>
    #include <string.h>

    void strcopy(char string1[], char string2[])
    {
    int i =0;
    int length;

    while ( string2[i] !='\0')

    {
    string1[i] = string2[i];
    ++i
    }
    string1[i] = '\o'

    length = strlen( string1 );

    while (string1[i] !='\o')

    {
    string2[i] = string1[length]
    ++i
    --length
    }
    while (string2[length] !='\o')
    return,

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >My question relates to writing a function that copies the
    >contents of a string and then replaces it in reverse order.
    So you want to copy a string, reverse it, and then replace the original string with the reversed copy? That's too much work for a simple operation, this should have the same result in a more efficient manner:
    Code:
    void reverse ( char *s )
    {
      int t, i, j;
      size_t len = strlen ( s ) - 1;
      for ( i = 0, j = len; i < j; i++, j-- )
        t = s[i], s[i] = s[j], s[j] = t;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM