#include <stdio.h> #include <stdio.h> int main() { int main() { char name[30]; char name[30], *ptr; printf("Enter name: "); ptr = name; gets(name); printf("Enter name: "); puts("Name:"); gets(ptr); puts(name); puts("Name:"); return 0; puts(ptr); } return 0; OUTPUT: } /rbin/ld: /tmp/ccAMyl5G.o: in function `main': JmNnE3oU2j.c:(.text+0x24): warning: the `gets' function is dangerous and should not be used. Enter name: devi arumugam Name: devi arumugam
getch and putch:
These functions are from <conio.h>, commonly used in Turbo C or MinGW on Windows. Not supported in modern compilers like GCC or Clang. Avoid Using gets(): gets is unsafe because it doesn't check for buffer overflows. Use fgets() ie. "file get string”, instead for safer input handling. Preferred Method: Use scanf or fgets for modern, secure programs. The fgets() function in C is a safer alternative to gets() for reading strings from input. It allows you to specify the maximum number of characters to read, which helps prevent buffer overflows. Syntax; char *fgets(char *str, int n, FILE *stream); E.g fgets(buffer, 50, stdin); How it works: fgets() reads up to n-1 characters from the specified stream and stores them in the array str. It stops reading when: A newline (\n) is encountered Example: #include <stdio.h> int main() { char name[30]; printf("Enter your name: "); fgets(name, sizeof(name), stdin); printf("Name: %s", name); return 0; } OUTPUT: Enter your name: devi arumugam Name: devi arumugam Without any warnings it just print it.
(ii) Calculating strings length
#include <stdio.h> #include <stdio.h> int main() { int main() { char name[] = "Alice Brown"; char name[50]; char *ptr = name; char *ptr = name; int length = 0; int length = 0; while (*ptr != '\0') { printf("Enter the string: "); length++; fgets(name, sizeof(name), stdin); ptr++; while (*ptr != '\0') { } if (*ptr == '\n') { printf("Length of the string: %d\ *ptr = '\0'; n", length); break; return 0; } } length++; OUTPUT: ptr++; Length of the string: 11 } printf("Length of the string: %d\ n", length); return 0; }
(iii) Copy string
#include <stdio.h> #include <stdio.h> void stringCopy(char *source, char #include <string.h> *destination) { void stringCopy(char *source, char while (*source != '\0') { *destination) { *destination = *source; while (*source != '\0') { source++; *destination = *source; destination++; source++; } destination++; *destination = '\0'; } } *destination = '\0'; int main() { } char source[50]; char destination[50]; int main() { printf("Eneter the name to char source[50]; copy:"); char destination[50 gets(source); printf("Enter the name to copy: stringCopy(source, destination); "); printf("Copied String: %s\n", fgets(source, sizeof(source), destination); stdin); return 0; } if (source[strlen(source) - 1] == OUTPUT: '\n') { source[strlen(source) - 1] = '\ 14 | gets(source); 0'; | ^~~~ } stringCopy(source, destination); | fgets printf("Copied String: %s\n", /rbin/ld: /tmp/ccyCNEOi.o: in destination); function `main': return 0; } Ycc4myk6mq.c:(.text+0x5e): warning: the `gets' function is dangerous and should not be used. Eneter the name to copy:devi arumugam Copied String: devi arumugam