i have written this program in c language to change any non-letter characters int an asteric '*' >> it do not gives me any compiling error but that what happens when i debug it here you are the code first : int main(){int i=0; char s1[50],s2[50]; printf("please enter the phrase"); gets(s1); while (s1[i]!='\0') {if (s1[i]>='a'&&s1[i]<='z') { s2[i]=s1[i];} else {s2[i]='*';} i++;} puts(s2);
and here the the task window
it does change the character but what are those missed up letters in the middle what is the error ?
you might want to have the output of s2 in array
here is the code in c++, it is similar to c, but it should work
#include <iostream> using namespace std; void main(){ int i =0,c; char s1 [50]; cout <<"how many character "; cin>>c; cout <<"Please Enter the Phrase ";cin>>s1; for (i = 0; i<c; i++){ { if (s1[i]>='a'&&s1[i]<='z'){ cout<<s1[i];} else { s1[i]= '*'; cout <<s1[i]; } } } cout<<endl; }
The problem is you never end the string `s2` with a null terminator, and so the program does not think the sting has ended yet and continues to print out whatever is in memory until it reaches `'\0'` (which is the `char` equivalent to the `int` value `0`). Consider putting `s2[i]='\0';` after the loop finishes and before printing `s2`.
Join our real-time social learning platform and learn together with your friends!