Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0d7ec13

Browse files
EASY/src/easy/ReadNCharactersGivenRead4.java
1 parente43ed48 commit0d7ec13

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
packageeasy;
2+
3+
/**
4+
* The API: int read4(char *buf) reads 4 characters at a time from a file.
5+
*
6+
* The return value is the actual number of characters read. For example, it returns 3 if there is
7+
* only 3 characters left in the file.
8+
*
9+
* By using the read4 API, implement the function int read(char *buf, int n) that reads n characters
10+
* from the file.
11+
*
12+
* Note: The read function will only be called once for each test case.
13+
*/
14+
15+
/**
16+
* The problem description is pretty ambiguous, actually the problem means to Keep reading until
17+
* either you have gotten n characters or there is no more characters to read.
18+
*/
19+
publicclassReadNCharactersGivenRead4 {
20+
publicintread(char[]buf,intn) {
21+
intindex =0,next=0;
22+
char[]buffer =newchar[4];
23+
while(index <n && (next =read4(buffer)) !=0){
24+
for(inti =0;i <next &&index <n;index++,i++){
25+
buf[index] =buffer[i];
26+
}
27+
}
28+
returnindex;
29+
}
30+
31+
privateintread4(char[]buffer) {
32+
//this is a fake method to make Eclipse happy
33+
return0;
34+
}
35+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp