Problem 19: Write a function that receives a pointer to a character string and r
ID: 1858797 • Letter: P
Question
Problem 19: Write a function that receives a pointer to a character string and returns the number of repeated characters that occur in the string. For example, the string, "Mississippi" has three repeated characters. Do not count repeated blanks in the string. If a character occurs more than two times, it should still only count as one repeated character; thus, "hisssss" would have only one repeated character. Assume that the function has the prototype statement
int repeat( char *ptr);
Write a program that contains a function. The function is the one described in problem 19. Test the function with your main function. Send a pointer to the following quote to your function:
"To those who do not know mathematics it is difficult to get across a real feeling as to the beauty, the deepest beauty, of nature. If you want to learn about nature, to appreciate nature, it is necessary to understand the language that she speaks in."
Then repeat the tests with this piece of a DNA sequence: "ATGCTAGTATTTGGATAGATAGATAGATAGATAGATAGATAAAAAAATTTTTTTT".
This is my code so far.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
//initialize and declare objects
int repeat(char *prt);
char strg1[] = "To those who do not know mathemtics it is difficult to get across a real feeling as to the beauty, the deepest beauty, of nature. If you want to learn about nature, to appreciate nature, it is necessary to understand the language that she speaks in.", strg2[] = "bb";
char *ptr1(strg1), *ptr2(strg2);
while ((ptr1=strstr(ptr1,ptr2)) != NULL)
{
count++;
ptr1++;
}
//Print the cout statements
cout << "Count: " << count << endl;
//initialize and declare objects- 2nd time through
int repeat(char *prt);
char strg1[] = "ATGCTAGTATTTGGATAGATAGATAGATAGATAGATAGATAAAAAAATTTTTTTT", strg2[] = "";
char *ptr1(strg1), *ptr2(strg2);
while ((ptr1=strstr(ptr1,ptr2)) != NULL)
{
count++;
ptr1++;
}
//Print the cout statements
cout << "Count: " << count << endl;
system("PAUSE");
return 0;
}
Explanation / Answer
For the function that counts the number of repeated characters in a string, at first I thought it would be much easier to do if you were to use a string object. I was going to recommend using string, since you're using C++. As it turns out, using a character array isn't too bad. I suppose your assignment calls for character arrays, so I'll show you how to do it that way as well.
Study and understand how I did it, and it should help you get your program working. Although my solution certainly works, it is by no means the only way of doing it. Maybe you can figure out a better approach.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
const char space = ' ';
size_t dupCount(const string &);
size_t dupCount(const char * const);
void rmchr(char * const, char);
int main(int argc, char *argv[]) {
string line;
while (true) {
cout << "Enter string: ";
getline(cin,line);
cout << dupCount(line) << " ";
cout << dupCount(line.c_str()) << endl;
line.clear();
}
return 0;
}
//
// Count duplicate non-space characters
//
size_t dupCount(const string &s0) {
size_t j = 0, x, n = 0;
string s = s0;
while (j < s.size()) {
if (((x = s.rfind(s.at(j))) != j) &&
(s[j] != space)) {
++n;
do {
s.erase(x,1);
} while ((x = s.rfind(s.at(j))) != j);
} else {
++j;
}
}
return n;
}
size_t dupCount(const char * const s0) {
size_t j = 0, n = 0;
char *s = new char[strlen(s0)+1], *p;
strcpy(s,s0);
while (j < strlen(s)) {
if (((p = strrchr(s,s[j])) != &s[j]) &&
(s[j] != space)) {
++n;
rmchr(s,*p);
} else {
++j;
}
}
delete s;
return n;
}
//
// rmchr( ): remove all 'a' from the string 's'
//
void rmchr(char * const s1, char a) {
char *ptr = s1;
char *sptr;
if (strchr(s1,a) != NULL) {
while (*ptr != '') {
if (*ptr == a) {
for (sptr = ptr; *sptr == a; ++sptr);
memcpy(ptr,sptr,&s1[strlen(s1)+1] - sptr);
}
if (*ptr != '') ++ptr;
}
}
}
Sample run:
Enter string:
0 0
Enter string: hello
1 1
Enter string: h e l l o
1 1
Enter string: mississippi
3 3
Enter string: xxxxx
1 1