-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path007_ManipulatingStrings.c
More file actions
58 lines (43 loc) · 1.38 KB
/
007_ManipulatingStrings.c
File metadata and controls
58 lines (43 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <string.h>
/** String things to remember:
*
* +The string.h header file defines many string functions:
* strcmp(), strstr() and so on
*
* +Ensure the string sotrage is large enough to hold the string + the null char at the end
* **/
int main()
{
/** Compute the lenght of a string using len **/
char string[] = "Just how long am I?";
int len;
len = strlen(string);
printf("The following string:\n");
puts(string);
printf("is %d characters long.\n\n", len);
/** Compute number of chars inserted **/
char input[64]; /*63 characters plus null */
int lenght;
printf("Write some instrunctions here: ");
/*fgets:
* gets the input and stores it in "input",
* reads up to 64 chars,
* stdin [standard input] is the source of the chars */
fgets(input, 64, stdin);
lenght = strlen(input);
printf("You typed %d characters of instructions.\n\n", lenght);
/** Combine some strings **/
/* Building a string in C
* 1. Create a char buffer (array) large enough to hold the new string
* 2. Copy the first string into the char buffer
* 3. Append or concatenate, the second string onto the first string in the char buffer.
* */
char first[] = "I would like to go ";
char second[] = "from here to there\n";
char buffer[50]; //step 1
strcpy(buffer, first); // step 2
strcat(buffer, second); //step 3
puts(buffer);
return (0);
}