so i'm trying print inputted string using putch , little bit of pointers.
here current code:
#include<stdio.h> #include<conio.h> #include<string.h> void printer(char *c); char *c; char ch; main(){ clrscr(); printf("enter string: "); scanf("%s",&ch); c = &ch; printer(c); getch(); } void printer(char *c){ int x; for(x=0;x<strlen(c);x++){ putch(*c); } }
the problem can print first character of string, reason strlen return 3 strings 3 characters , below.
do have use array can use putch since limited 1 character output.
one of problems printer() function not printing other first character. there 2 ways of approaching this. using pointers:
void printer(char const *c){ while ( *c != '\0' ) { putch(*c); c++; } }
and using pointer arithmetic:
void printer(char const *c) { int x; ( x=0; x < strlen(c); x++ ) { putch( *(c + x) ); } }
the biggest problem attempting store string in single character in memory. that's asking problems.
char ch; scanf("%s",&ch); // no no no no no
instead declare buffer (to store string in) array big enough biggest string expect:
char ch[512]; scanf("%s", ch);
Comments
Post a Comment