-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5.rs
88 lines (72 loc) · 1.88 KB
/
5.rs
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/// @brief Copies at most n chars from string s to t.
///
/// @param s Source string
/// @param t Target string
/// @param n Number of characters
fn pstrncpy(s: &str, t: &mut String, n: i32)
{
let mut i: i32 = 0;
for _c in s.chars() {
if i >= n {
break;
}
t.remove(i as usize);
t.insert(i as usize, _c);
i += 1;
}
}
/// Copies at most n chars from string s to the end of t.
///
/// @param s Source string
/// @param t Target string
/// @p
fn pstrncat(s: &str, t: &mut String, n: i32)
{
let mut i: i32 = 0;
for _c in s.chars() {
if i >= n {
break;
}
t.push(_c);
i += 1;
}
}
/// Compares at most n chars from string s and t.
///
/// @param s Source string
/// @param t Target string
/// @param n Number of characters to be matched
///
/// @return bool True if they match, false otherwise
fn pstrncmp(s: &str, t: &str, n: i32) -> bool
{
let mut i: i32 = 0;
let mut s_iter = s.chars();
let mut t_iter = t.chars();
// Check if characters match
while (s_iter.next() == t_iter.next()) && i != n {
i += 1;
}
// If i == n all chars are matched
if i == n {
return true;
}
false
}
/// Implementing a strcat function.
fn main ()
{
let mut t1 = String::from("The first string is");
let s1 = String::from(" IN NEED OF COMPLETION!");
let mut t2 = String::from("abcdef678910");
let s2 = String::from("12345");
let s3 = String::from("123456789");
print!("\"{}\"\t(pstrncat)=>\t", t1);
pstrncat(&s1, &mut t1, 24);
print!("\"{}\"\n", t1);
print!("\"{}\"\t(pstrncpy)=>\t", t2);
pstrncpy(&s2, &mut t2, 5);
print!("\"{}\"\n", t2);
print!("\"{}\"\tpstrncmp\t\"{}\"=>\t{}\n", s2, s3, pstrncmp(&s2, &s3, 5));
print!("\"{}\"\tpstrncmp\t\"{}\"=>\t{}\n", t1, s2, pstrncmp(&t1, &s2, 5));
}