given a new string and a position...i need to enter the new string into another string...
Printable View
given a new string and a position...i need to enter the new string into another string...
simple, try this
Code:
/* Here i is the position of where to copy the newstring into another string */
strcpy((another_string+(i-1)), newstring) ;
To add a string into another one, you should look at strcat().
#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...
For inserting in between the string u need to take one more temp variable
Try this,
N'joyCode:
#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.*/
void main(void)
Never use void main(), you should use instead int main()
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 ???
http://www.cprogramming.com/boardfaq.html#main
or seach the board on void main
--- END OF DISCUSSION ---
>>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 :-)