Jump to content

Problem in String replace program's output

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
suvojit168

suvojit168

    Newbie

  • Members
  • Pip
  • 3 posts
Hi all,
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 have coded it but the output generated is not correct.

Following is the code:
#include<stdio.h>

#include<conio.h>

void str_replace(char *str,char *str1)

{

	puts(str);

	puts(str1);

	while(*str1!='\0')

	{

		*str=*str1;

		++str1;

		++str;

	}

	printf("\n\n String after replacement");

	puts(str);

}

void main()

{

	char *str,*str1;

        clrscr();

	printf("\n Enter the original string");

	gets(str);

	printf("\n Enter the string with which you want to replace your original string \n\n");

	gets(str1);

	str_replace(str,str1);

getch();

}

Output
Enter the original string suvo
Enter the string with which you want to replace your original string k
String after replacement uvo


As you can see the program generates uvo instead of kuvo.
Help to fix it

Thanks

#2
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
Your program seems really very dangerous. You declared 2 pointers to char, but you didn't even allocated memory for them, and just after that you use gets, which will get the input into an char array (that isn't even allocated), and gets itself is the most unsafe function in the world, it can overflow your buffer easily, because there is no limit on the input. You also used the non standard conio.h library and some non standard functions like clrscr(), i assume that you're using turbo C, a very old compiler, use GCC instead. And there is no such thing as void main(), but int main() or int main(int argc, char** argv), void main is not standard, and you should not use it because void main doesn't return a value to the operating system which can mean a successful execution or error for example.

So, start to replace that pointers for something like:

char str[50];
char str1[50];

Replace gets for:

fgets(str1, 50, stdin); // get max 50 chars into str1, totally safe, can't overflow.

and void main() for:

int main()

and in the end of int main function:

return 0;


also, take out that conio.h library, that non standard function clrscr() and that getch() function. use getchar() instead of getch(), or maybe just nothing (the best way).

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
There are a lot of other problems with this as well. Your pointer manipulation means you change strings, and no longer no where the beginning of the string is. The result is you start printing AFTER the last character that was replaced.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog