Thread: enter a string into another string

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    15

    enter a string into another string

    given a new string and a position...i need to enter the new string into another string...

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    10
    simple, try this

    Code:
    /* Here i is the position of where to copy the newstring into another string */
      strcpy((another_string+(i-1)), newstring) ;
    "There is no alternative to consitency..."

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    To add a string into another one, you should look at strcat().

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    15
    #include <string.h>
    #include <stdio.h>

    void main(void){

    char newstring[20] = "lee";
    char another_string[20] = "This is kenny";

    int i=9;

    strcpy((another_string+(i-1)), newstring) ;

    printf("%s",another_string);
    }

    This would print "This is lee"...instead of "This is lee Kenny"..which is the correct output...

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    10
    For inserting in between the string u need to take one more temp variable
    Try this,

    Code:
    #include <string.h>
    #include <stdio.h>
    
    void main(void){
    
    char newstring[20] = "lee";
    char another_string[20] = "This is kenny";
    char temp_string[20] ;
    
    
    int i=9;
    int size=strlen(newstring);
    
    strcpy( temp_string,another_string);
    strcpy((another_string+(i-1)), newstring ) ;
    strcat(another_string, " ") ;
    strcat(another_string, (temp_string+i-1)) ;
    
    printf("%s",another_string);
    
    }
    
    /*Pls. take care of error handling,
    u can write this code in multiple ways...try out other ways also.*/
    N'joy
    "There is no alternative to consitency..."

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    void main(void)

    Never use void main(), you should use instead int main()

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    10
    What is the exact reason for not using void main.

    As far as i understand, people say int main should be used because a value should be returned to the OS.

    But i think compiler dose take care of this..so where is the problem ???
    "There is no alternative to consitency..."

  8. #8
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    http://www.cprogramming.com/boardfaq.html#main

    or seach the board on void main

    --- END OF DISCUSSION ---

  9. #9
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>What is the exact reason for not using void main.
    Because the C language states that it's wrong. But, if you don't give a hoot about being correct or portable and your compiler supports void main I don't see a problem :-)
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM