@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