-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsingStr.cpp
More file actions
74 lines (68 loc) · 1.82 KB
/
parsingStr.cpp
File metadata and controls
74 lines (68 loc) · 1.82 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* File: parsingStr.cpp
* Author: Ashley Lu Couch
* Assignment: Lab 4
* Course: CSI 1440
* Due Date: 2/13/2019
*
* Date Modified: 3/25/2019
* -Redid the pages that were missing
*
* Date Modified: 2/13/2019
* -created
*
* This Program is four functions that let a user parse strings
* The Functions are: mostFrequent(), leastFrequent(),
* moveToEnd(), and stuGetLine()
*
*/
#include "parsingStr.h"
char mostFrequent(char *string) {
int charCounter[127] = {0};
int index = 0, indexMost = 0;
while (string[index] != '\0') {
++charCounter[(int) string[index]];
++index;
}
// go through the printable characters
for (int i = 33; i < 127; ++i) {
if(charCounter[i] > charCounter[indexMost]){
indexMost = i;
}
}
return (char)indexMost;
}
char leastFrequent(char *string) {
int charCounter[127] = {0};
charCounter[0] = 100000000;
int index = 0, indexLeast = 0;
while (string[index] != '\0') {
++charCounter[(int) string[index]];
++index;
}
// go through the printable characters
for (int i = 33; i < 127; ++i) {
if(charCounter[i] < charCounter[indexLeast] && charCounter[i] > 0){
indexLeast = i;
}
}
return (char)indexLeast;
}
void moveToEnd(char *string, char *ch) {
int index = 0;
while (&string[index] != ch && string[index] != '\0'){
++index;
}
while (string[index + 1] != '\0'){
swap(string[index], string[index + 1]);
++index;
}
}
void stuGetLine(char *stringR, const char *string, char delim, int capacity) {
int index = 0;
while(string[index] != delim && string[index] != '\0' && index+1 < capacity ){
stringR[index] = string[index];
++index;
}
stringR[index] = '\0';
}