Reading and writing strings
Can be done Manually using getch and putch
#include <stdio.h> #include <stdio.h>
int main() { int main() {
char name[30], ch; char name[30], *ptr;
int i = 0; char ch;
printf("Enter name: "); ptr = name;
while (ch != '\n') { printf("Enter name: ");
ch = getchar(); while ((ch = getchar()) != '\n') {
if (ch != '\n') { *ptr = ch;
name[i] = ch; ptr++;
i++; }
} *ptr = '\0';
} printf("Name: %s\n", name);
name[i] = '\0'; return 0;
printf("Name: %s\n", name); }
return 0;
} OUTPUT:
OUTPUT: Enter name: devi_arumugam
Enter name: devi_arumugam Name: devi_arumugam
Name: devi_arumugam Enter name: devi arumugam
Enter name: devi arumugam Name: devi arumugam
Name: devi arumugam
But using printf and scanf
#include <stdio.h> #include <stdio.h>
int main() { int main() {
char name[30]; char name[30], *ptr;
printf("Enter name: "); ptr = name;
scanf("%s", name); //scanf("%[^\ printf("Enter name: ");
n]", name); scanf("%s", ptr);
printf("Name: %s\n", name); printf("Name: %s\n", ptr);
return 0; return 0;
} }
OUTPUT:
Enter name: devi arumugam
Name: devi
Enter name: devi arumugam
Name: devi arumugam
Using gets and puts
#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
(iv) String concatenation
#include <stdio.h>
void stringConcat(char *str1, char *str2) {
while (*str1 != '\0') {
str1++;
}
*str1 = ' ';
str1++;
while (*str2 != '\0') {
*str1 = *str2;
str1++;
str2++;
}
*str1 = '\0';
}
int main() {
char str1[50] = "Hello";
char str2[] = "World!";
stringConcat(str1, str2);
printf("Concatenated String: %s\n", str1);
return 0;
}
OUTPUT
Concatenated String: Hello World!
(v) Comparing strings
#include <stdio.h>
int stringCompare(char *str1, char *str2) {
while (*str1 != '\0' && *str2 != '\0' && *str1 == *str2) {
str1++;
str2++;
}
return *str1 - *str2;
}
int main() {
char str1[] = "Hello";
char str2[] = "Hello";
int result = stringCompare(str1, str2);
if (result == 0) {
printf("Strings are equal\n");
} else if (result < 0) {
printf("String 1 is less than String 2\n");
} else {
printf("String 1 is greater than String 2\n");
}
return 0;
}
OUTPUT:
Strings are equal
(vi) String reversing
#include <stdio.h>
void stringReverse(char *str) {
char *start = str;
char *end = str;
char temp;
while (*end != '\0') {
end++;
}
end--;
while (start < end) {
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
int main() {
char str[] = "Hello";
stringReverse(str);
printf("Reversed String: %s\n", str);
return 0;
}