-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelloStrings.c
More file actions
45 lines (39 loc) · 1.64 KB
/
helloStrings.c
File metadata and controls
45 lines (39 loc) · 1.64 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
#include <stdio.h>
//needed for strlen function
#include <string.h>
/*
saveMessage opens the file indicated by fileName for writing
and writes the message to the file indicated by fileName
recall: const is a promise that your function won’t alter the contents of the array (or variable)
*/
void saveMessage(const char fileName[], const char message[]){
//your implementation here
}
int main(void){
//declare and initalize a string of size 100
//remember that it will insert the null (0) terminating byte for you
char text[100] = "this is my default text";
//just to experiment, let's write a loop that will go through the array and show the contents
int i;
printf("the ascii codes of each character in this string are: \n");
for(i = 0 ; i < 100; i++){
printf("%d ", text[i]);
}
printf("<- end of string\n");
//a different easier way to diplay it would be with printf and %s
printf("the default text is -> %s <-\n", text);
//let the user enter a different text to store in the string
printf("please enter a string 99 characters or less: ");
/*
the 99 limits input to 99 characters and the [^\n] part will stop reading characters at the newline
the newline itself is not copied into the string
usually we would clear the buffer after this to get rid of any other stuff in the buffer (ex. '\n')
notice the space between " and the % do this in order to ignore any leading whitespace
*/
scanf(" %99[^\n]", text);
int stringLength = strlen(text);
printf("the new string entered is: %s its length is: %d\n", text, stringLength);
//use the saveMessage function to save the user-entered string to a file
saveMessage("data.txt", text);
return 0;
}