On success, the total number of characters written is returned.
On failure, a negative number is returned.
Input/Output
#include<stdio.h>intmain(){
char buf[50];
int n;
int a = 5;
int b = 3;
n = sprintf(buf, "%d + %d = %d", a, b, a+b);
printf("%s\n", buf);
printf("%d\n", n);
return0;
}
Output:
5 + 3 = 8
9
Input/Output
scanf
Precise input formatting can be accomplished with scanf
scanf(format_control_string, other_arguments);
format_control_string describes the formats of the input.
ither_arguments are pointers to variables in which the input will be stored.
Input/Output
// year, month, and day are "int"scanf("%d-%d-%d", &year, &month, &day);
// year, month, and day are "int"scanf("%d%*c%d%*c%d", &year, &month, &day);
// character is a "char"scanf("%c\n", &c);
// string is a "char" arrayscanf("%s", string);
Input/Output
gets
char *gets(char* str)
Reads a line from stdin and stores it into the string pointed to by str.
It stops when either the newline character is read or when the end-of-file is reached, whichever comes first.
structstudent{char name[20];
char gender;
int age;
structstudent* next;
};
typedefstructstudentStudent;
Use typedef when defining the structure.
typedefstructstudent{char name[20];
char gender;
int age;
structstudent* next;
} Student;
Exercise 9
計算文章出現的每個單字所出現的次數
所有標點符號及數字都不能出現
輸入不只一行
所有的單字都要用小寫來計算(例:The和the視為同一個單字)
輸出的單字順序要用字典排序
Get exercise 9 folder by command line
oj get_assign ex9
Submit your exercise 9 script by command line
oj submit ex9 <your_script_file>
Input
I have a pen. I have an apple.
Uhh!! Apple-pen.
I have a pen. I have a pineapple.
Uhh!! Pineapple-pen.
Apple-pen. Pineapple-pen.
Uhh!! Pen pineapple apple pen.
Output
a 3
an 1
apple 4
have 4
i 4
pen 8
pineapple 4
uhh 3
Assign9 Hint
因為打英文太累,而且麒竑的英文頗爛,所以我打中文
Assign9 Hint
輸入格式
車站
站數
停靠站及停靠順序(由北往南或由南往北,不會有亂跳的狀況)
訂位
RESERVE NAME, FROM, TO, #tickets, SEAT(s)
取消
CANCEL NAME, FROM, TO, SEAT
查詢
CANCEL NAME, SEAT
Assign9 Hint
輸出格式
訂位
RESERVE SUCCESSED!! -> NAME SEAT (FROM - TO)
RESERVE FAILED.... (station information has something wrong)
RESERVE FAILED.... (too many seats)
RESERVE FAILED.... (repest seats)
取消
CANCELLATION SUCCESSED!! SEAT (FROM - TO)
CANCELLATION FAILED.... (cannot find the stations information)
CANCELLATION FAILED.... (cannot find the seat information)
查詢
CHECK NAME SEAT -> (FROM - TO)
CHECK FAILED.... (cannot find the reservation data)