Problem in String replace program's output

Posted on 16th Feb 2014 by admin

Hi all,
I was trying to make a program which accepts a string and replaces it with another string accepted using pointers.The code following is error free but on executing it doesnot produce correct result.I have supplied the output below the code. Help me to fix the error.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include #include void main() { char *str,*str1; clrscr(); printf("n Enter the string"); gets(str); printf("n Enter the string with which you want to replace 1st string"); gets(str1); printf(" string are : t"); puts(str); printf("t"); puts(str1); while(1) { if(*str1 !='') { *str=*str1; ++str; ++str1; } if(*str1 =='') { break; } } printf("n New String is t"); puts(str); getch(); }
Output Enter the string Suvo Enter the string with which you want to replace 1st string k string are Suvo k New String is uvo
Also please tell me how can I print the current character pointed bu a char pointer(string). For testing the behavior of pointers inside the while loop I tried using printf but it shows weird chars like splecial chars(play button, heart, spade etc)

Thanks in advance

Other forums