Problem in String replace program's output

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<stdio.h>
#include<conio.h>
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 !='\0')
		{
			*str=*str1;
			++str;
			++str1;
		}
		if(*str1 =='\0')
		{
			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
closed account (S6k9GNh0)
1. Do not use gets or puts. They're both pathetic excuses for standard functions and very insecure although convenient. Use printf or cout for output, and scanf or cin for input as they're secure and less prone to mistakes.

2.If your using C++, use cstdio.h instead of stdio.h. I honestly don't know the difference other than that's the correct way to do things...

3. The while loop is meant to check for a boolean. 1, though it returns true and goes forever, isn't the most correct way to do things and some compilers actually check to see if 1 is true which uses unneccessary resources. For your example, for(;;) would be a lot more effecient and a quick optimization. Or you could set a boolean, have the while loop check the boolean each time, and when you want to to end the loop, set the boolean to false.

4. Last but not least, I have no clue what you mean by replace the first string. This seems like such a relative task that has been overly complicated, that I'm not sure how to react.
Last edited on
Don't use scanf unless you need formatted input. Also, it isn't safe unless you know how to use it; I remember once on windows I caused a stop error with it. I can't remember what I did though... :)

If you don't need formatted input then use fgets() it's alot faster.

1
2
3
4
5
6
7
8
9
10
11
12
13
	while(1)
	{
		if(*str1 !='\0')
		{
			*str=*str1;
			++str;
			++str1;
		}
		if(*str1 =='\0')
		{
			break;
		}
	}


Wouldn't
1
2
3
4
5
while (*str != '\0') {
    *str = *str1;
    str++;
    str1++;
}


or even
for (; (*str = *str1) != '\0'; str++, str1++);
make more sense? The parentheses around *str = *str1 are necessary; != has a higher precedence than '\0'.

In fact if you have access to the C library you don't even need to do that. The content of strcpy(char* dest, const char* src) is probably something very similar to one of my loops above. You could just strcpy(str, str1); if you really want to.

computerquip; you use cstdio because C++ std lib headers don't have .h at the end. Don't ask why; probably something ANSI came up with.

Actually; that for loop should be
for (; *str = (*str1 != '\0'); str++, str1++);
It shouldn't make much difference but you're comparing *str1 to NULL afterall, so it makes more sense.
Last edited on
closed account (S6k9GNh0)
Great loop examples. I would have written one but my brain is starving for food right now and I refuse to spend four bucks on school lunch T.T.

@ cstdio: That's it? No different definitions for C++? Isn't NULL defined differently in cstdio?
@ cstdio: That's it? No different definitions for C++? Isn't NULL defined differently in cstdio?

Possibly. You could, you know, open cstdio in a text editor. :)

Four dollars... that's £2.40! That's ridiculous! It's £1.60 ($2.65) here and I think that's a rip off!
Last edited on
@computerquip (474): Thanks for your valuable suggestion, esp I dint knew the fact about while(1). Thanks again to enlighten me.I will keep all this in mind in future.
Well I will explain my problem again. I want to make a program which will first accept a string from user. Now this string will have to be replaced by another string accepted. Something like this:

Assuming user enters string: "Suvo"
and he wants it to be replaced by "k" then program should replace S with k and the result generated should be "kuvo".
similarly if user wants it to be replaced by "ka" then aprogram should replace "S" with "k" and "u"with "a" the result generated should be "kavo".
I hope I have explained it in a better way now.

@chrisname: I thank you for your valuable suggestions. Its really helpful.
First:I tried using fgets() and fputs() but its giving error Too few parameters-It expects a File pointer along with the string. So can you tell me the correct way to use it. I dont need a File pointer in this program I guess.

Second: I know that I can use the string functions but since I am a student so I am required to try these using pointers( so that we can be comfortable with pointers).

Third: I agree with you and computerquip. My looping logic is not at all perfect. The one you suggested is good and simple and definitely makes much more sense.

Fourth: I cant understand the for loop logic which you proposed, can you please explain it.


@both: I have recoded the program and its executes w/o errors, but the problem I reported in my earlier post remains unsolved.
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
32
33
34
35
36
37
#include<stdio.h>
#include<conio.h>
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 (*str1 != '\0')
	{
		*str = *str1;
		str++;
		str1++;
	}
	/*for (;;)
	{
		if(*str1 !='\0')
		{
			*str=*str1;
			++str;
			++str1;
		}
		if(*str1 =='\0')
		{
			break;
		}
	}*/
	printf("\n New String is \t");
	puts(str);
	getch();
}


I have used both the looping logic suggested and both working fine but I prefer the while loop logic. The desired output of the program should be :
Output
Enter the string Suvo

Enter the string with which you want to replace 1st string k

string are Suvo 
          k

New String is Kuvo


but instead of this output generated is :
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


It will be great if you guys can run this code at your machines and you will know what I am trying to say.
There is some logic error perhaps I did some rudimentary mistakes in applying pointers and I cant figure out that mistake. Pls help me to fix this error so that I can get the desired output.

Thanks

fgets(pointer to string, amount of characters, file);
File will be stdin or some file of type FILE*.
Topic archived. No new replies allowed.