Print a string using putch with pointers in C

So, I'm trying to print the input string using putch and some pointers.

Here is my current code:

#include<stdio.h> #include<conio.h> #include<string.h> void printer(char *c); char *c; char ch; main(){ clrscr(); printf("Enter a 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 is that I can only print the first character of a string, also for some reason strlen always returns 3 for strings that have 3 characters or lower.

Do I need to use an array for this so that I can use putch since it is limited to only 1 character output.

+6
source share
6 answers

One problem is that your printer () function does not print anything except the first character. There are two ways to approach 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; for ( x=0; x < strlen(c); x++ ) { putch( *(c + x) ); } } 

The biggest problem is that you are trying to save a string in one character in memory. This is just a task.

 char ch; scanf("%s",&ch); // NO NO NO NO NO 

Instead, declare your buffer (to save the string in) as an array large enough for the largest string you expect:

 char ch[512]; scanf("%s", ch); 
+2
source

try this code:

 #include<stdio.h> #include<conio.h> #include<string.h> void printer(char *c); char *c; char buffer[1000];// use as a buffer void main(){ clrscr(); printf("Enter a string: "); scanf("%s",buffer);//read the input to the buffer c=(char*)malloc(strlen(buffer)+1);//alloc memory with len of input + 1 byte to "\0"(end of string) strcpy(c,buffer);//copy the input from the buffer to the new memory printer(c); getch(); free(c);//free the memeory } void printer(char *c) { int x; for(x=0;x<strlen(c);x++){//move the index string pointer to next char in the string putch(c[x]);//print the char to the screen } } 

1) You cannot use char to save a string u need char * !!!

2) You can get input into memory that is not allocated !!!! because of this, u must read the input into the buffer after this destination line by the size of the input inside the buffer!

+2
source

Your code prints only the first character, because c always points to the first character of the array. To print a common line, you also need to increase the character pointer. You need to do this:

 void printer(char *c){ while(*c != '\0'){ putch(*c); c++; } } 
0
source

First, you pass a pointer to the "single-character repository" to scanf . Everything that happens after that is in the territory of nsal demons .

Secondly, scanf does not allocate storage for your input, so even if you sent c instead of &ch , you would not be better off.

Third, you really need to declare variables inside main instead of using global variables.

Something like this might be closer to what you really want:

 void output (char *c) { char *cp; for (cp = c; *cp; cp++) { putch(*cp); } } int main (void) { char input[80]; printf("Please input a string: "); scanf("%s\n", input); output(input); } 
0
source

First calculate the length of the string, and then use the above implementation like this -

 void printer(char *c){ int i, length; length=strlen(c) for(i=0;i<lenth;i++,c++){ putch(*c); } } 

It should work, I think.

0
source
  #include<stdio.h> #include<conio.h> #include<string.h> void printer(char *c); char *c; char ch;//the ch should be a array main(){ clrscr(); printf("Enter a string: "); scanf("%s",&ch);//the ch don't need '&' c = &ch;//the ch don't need '&' printer(c); getch(); } void printer(char *c){ int x; for(x=0;x<strlen(c);x++){ putch(*c); } } 
-1
source

Source: https://habr.com/ru/post/949214/


All Articles