-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcstrings.cpp
More file actions
157 lines (136 loc) · 3.6 KB
/
Copy pathcstrings.cpp
File metadata and controls
157 lines (136 loc) · 3.6 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//Amelie Cameron Assignment 7 CStrings
// This program implements functions that deal with null terminated C-Style strings.
// The 6 functions accomplish the following: 1-return the last index ofa target char, 2-reverses any string passed in, 3-replaces all target chars with a replacement char, 4-finds the first index of a substring in any given string, 5-finds if a given string is a palindrome, 6- EXTRA reverses the words in a given string.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int lastIndexOf(char *s, char target)
{
int index = 0;
int size = strlen(s);
for(int i = 0; i < size; i++)
{
if(s[i] == target)
index = i;
}
return index;
}
void reverse2(char *s,int begin,int end)
{
int i, j;
for(i = begin, j = end; i<j; i++, j--)
{
char temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
void reverse(char *s)
{
reverse2(s, 0, strlen(s)-1);
}
bool isPalindrome(char *s)
{
int size = strlen(s);
char n[size];
strcpy(n, s);
reverse(n);
for(int i = 0; i < size; ++i)
{
if(s[i] != n[i])
return false;
}
return true;
}
int replace(char *s, char target, char replacementChar)
{
int size = strlen(s);
int count = 0;
for(int i = 0; i < size; i++)
{
if(s[i] == target)
{
s[i] = replacementChar;
count++;
}
}
for(int i = 0; i < size; i++)
cout << s[i];
cout << endl;
cout << "Replacements made: " << count << endl;
return count;
}
int findSubString(char *s, char substring[])
{
int size = strlen(s);
int subsize = strlen(substring);
char compare[subsize+1];
int substringIndex = -1;
for(int i = 0; i < size; i++)
{
if(s[i] == substring[0])
{
strncpy(compare, s + i, subsize);
compare[subsize] = '\0';
if(strcmp(substring, compare) == 0)
substringIndex = i;
}
}
return substringIndex;
}
void reverseWords(char *s)
{
int length = strlen(s);
reverse(s);
int lastWhitespace = 0;
for(int i = 0; i < length; i++)
{
if(s[i] == ' ')
{
reverse2(s, lastWhitespace, i-1);
lastWhitespace = i + 1;
}
}
}
int main()
{
char str[] = "This is a sample string";
char target = 's';
int lastIndex;
char replacementChar = 'A';
cout << endl;
cout << "Original string: " << str << endl;
lastIndex = lastIndexOf(str, target);
cout << endl;
cout << "#1: Last index of the char '" << target << "' in the string is: " << lastIndex << endl;
cout << endl;
cout << "#2: The reversed form of the string: " << endl;
reverse(str);
for(int i = 0; i < strlen(str); ++i)
cout << str[i];
cout << endl;
cout << endl;
cout << "#3: Replace target char '" << target << "' with '" << replacementChar << "': "<< endl;
replace(str, target, replacementChar);
cout << endl;
char amelie[] = "amelie";
char substring[] = "li";
cout << "The second sample string is '" << amelie << "' and the substring is '" << substring << "'"<< endl;
cout << "#4: First index of substring '" << substring << "' (-1 if n/a): ";
cout << findSubString(amelie, substring) << endl;
isPalindrome(str);
char pal[] = "abcba";
cout << endl;
cout << "The string array named pal is: " << pal << endl;
cout <<"#5: Is the string array named 'pal' a palindrome? (1 True, 0 False): " << isPalindrome(pal) << endl;
cout <<endl;
char extrastring[] = "This is another sample string";
cout << "Extra string is: " << extrastring << endl;
reverseWords(extrastring);
cout << "#6: Extra Credit: Reversed words in string: " << endl;
for(int i = 0; i < strlen(extrastring); i++)
cout << extrastring[i];
cout << endl;
return 0;
}