replace string with selected characted into a new character.

so i did random generator of letters.

so i got s1=qwerty
then i imput " Character you want to change"
so i put s2=w
then i input " character you want to change it with"
so i put s3= $

....

so far all its doing is pulling out the characters that i do want to change and changes them, but the rest of the string is not there. the characters are gone.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <time.h>
#include <string.h>
#include <iostream>
#define MIN 0
#define MAX 25

int getrand(int min, int max);
int main(void) { 

char s1[256], s2[256];
char string[256];
int p,c,i=0; 
srand(time(NULL));

for(i; i<40; i++) {
p=getrand(MIN,MAX);
p=p+65;
putchar (p);

if ((p >= 'A') || (p <= 'Z'))
{
s1[i] = ("%c",p);
}
}

if (i == 40)
{
s1 [i] = '\0';
}
printf (" \n");
printf (" \n");

printf ("Enter a the letters youd like to change: ");
gets_s (s2);
printf ("Enter a the character youd like to change it to: ");
c = getchar ();
printf (" \n");
printf (" \n");

char * pch;
printf ("'%s' changed to:\n ",s1);
pch = strpbrk (s1, s2);
while (pch != NULL)
{
*pch = c;
printf ("%c " , *pch);
pch = strpbrk (pch+1,s2);
}
printf ("\n");

return 0;

}
int getrand(int min,int max){ 
return(rand()%(max-min)+min);
}


--------------------------------------...
as result i get:

LAOUHKJUSAALKMRBHJOJJQUCQPSNGFECXQGFKX...

Enter a the letters youd like to change: A
Enter a the character youd like to change it to: #


'LAOUHKJUSAALKMRBHJOJJQUCQPSNGFECXQGFK... changed to:
# # #
Press any key to continue . . .

***but its supposed to be
***'L#OUHKJUS##LKMRBHJOJJQUCQPSNGFECXQGFK'
*** but it wont budge!.
*** ive triend many functions, and i just cant get it anymore.
closed account (S6k9GNh0)
I'll go ahead and tell you. Your code is bugged, not very fun to read, and annoys the hell out of me. You have things in there that it seems you got desperate and placed random things in there! But don't lose hope...

char string[256]; This isn't a good idea. If someone sees this they'll eat your brains.

1
2
3
4
5
6
7
8
9
10
11
	for(i; i < 40; i++) 
	{
		p =		getrand(MIN,MAX);
		p += 	65;
		putchar (p);

		if ((p >= 'A') || (p <= 'Z'))
		{
			s1[i] = ("%c",p); //Forgot to place your function.
		}
	}

Forget something? PrintF won't work here anyways.
sprintf(&s1[i], "%c", p); //Try this
:p
1
2
3
4
5
	
if (i == 40)
{
	s1 [i] = '\0';
}

There is no reason for this if statement unless it to make sure it was completed which you didn't do or attempt.

 
printf (" \n");

Does nothing but clutter code and irritate the user. Please, oh please, don't spam these.
gets_s (s2);
This doesn't exist. Try gets(s2);

And last but not least, char * pch cannot be assigned a value of c. Especially since it points to a char. And there are other things I just haven't the time right now to explain.

Last edited on
Topic archived. No new replies allowed.