Skip to content

Commit 9eb4ab9

Browse files
strings
1 parent c23abb0 commit 9eb4ab9

6 files changed

+75
-0
lines changed

Diff for: strings/string_declaration.c

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
char str_1[6] = "hello"; // str length + 1 for '\0' end character.
5+
char str_2[] = "world"; // auto calc str length.
6+
char str_3[6] = {'h', 'e', 'l', 'l', 'o', '\0'}; // add '\0' end character manually.
7+
char *str_p = "stuff"; // string pointer declaration, is considered a constant.
8+
9+
return 0;
10+
}
11+
/*
12+
A string literal is a text enclosed in double quotation marks.
13+
A character, such as 'b', is indicated by single quotation marks and cannot be treated as a string.
14+
*/

Diff for: strings/string_input.c

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
char first_name[25];
5+
int age;
6+
printf("Enter your first name and age: ");
7+
scanf("%s %d", first_name, &age); // an array name acts as a pointer.
8+
printf("Hello %s, you're %d", first_name, age);
9+
// scanf() stops reading input when it reaches a space.
10+
return 0;
11+
}

Diff for: strings/string_input_2.c

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
/*To read a string with spaces, use the gets() function.
5+
It reads input until a terminating newline is reached (the Enter key is pressed).*/
6+
printf("Enter your full name: ");
7+
char full_name[45];
8+
gets(full_name);
9+
printf("Your full name es %s\n", full_name);
10+
return 0;
11+
}

Diff for: strings/string_input_3.c

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
/*
5+
A safer alternative to gets() is fgets(), which reads up to a specified number of characters.
6+
This approach helps prevent a buffer overflow, which happens when the string array
7+
isn't big enough for the typed text.
8+
*/
9+
printf("Enter your full name: ");
10+
char full_name[45];
11+
fgets(full_name, 45, stdin); // fgets() reads only n-1 characters from stdin because there must be room for '\0'.
12+
printf("Your full name es %s\n", full_name);
13+
return 0;
14+
}
15+
/*
16+
The fgets() arguments are the string name, the number of characters to read,
17+
and a pointer to where you want to read the string from.
18+
stdin means to read from the standard input, which is the keyboard.
19+
Another difference between gets and fgets is that the newline character is stored by fgets.
20+
*/

Diff for: strings/string_output.c

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
char city[40];
5+
printf("Enter your favorite city: ");
6+
fgets(city, 40, stdin);
7+
fputs(city, stdout);
8+
printf(" is a fun city.\n");
9+
return 0;
10+
}

Diff for: strings/string_output_2.c

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
char city[40];
5+
printf("Enter your favoritce city: ");
6+
fgets(city, 40, stdin);
7+
puts(city); // adds a newline to output.
8+
return 0;
9+
}

0 commit comments

Comments
 (0)