Question

In: Computer Science

C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings....

C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings. Assume that any char array passed into the functions will contain valid, null-terminated data. The functions must have the signatures listed below.

1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function returns 2.

int lastIndexOf(char *s, char target)

2. This function alters any string that is passed in. It should reverse the string. If “flower” gets passed in it should be reversed in place to “rewolf”. To be clear, just printing out the string in reverse order is insufficient to receive credit, you must change the actual string to be in reverse order.

void reverse(char *s)

3. This function finds all instances of the char ‘target’ in the string and replaces them with ‘replacementChar’. It also returns the number of replacements that it makes. If the target char does not appear in the string it returns 0 and does not change the string. For example, if s is “go giants”, target is ‘g’, and replacement is ‘G’, the function should change s to “Go Giants” and return 2.

int replace(char *s, char target, char replacementChar)

4. This function returns the index in string s where the substring can first be found. For example if s is “Skyscraper” and substring is “ysc” the function would return 2. It should return -1 if the substring does not appear in the string.

int findSubstring(char *s, char substring[])

5. This function returns true if the argument string is a palindrome. It returns false if it is not. A palindrome is a string that is spelled the same as its reverse. For example “abba” is a palindrome. So is “hannah”, “abc cba”, and “radar”.

bool isPalindrome(char *s)

Don't get confused by white space characters. They should not get any special treatment. “abc ba” is not a palindrome. It is not identical to its reverse.

6. This function should reverse the words in a string. A word can be considered to be any characters, including punctuation, separated by spaces (only spaces, not tabs, \n etc.). So, for example, if s is “The Giants won the Pennant!” the function should change s to “Pennant! the won Giants The”

void reverseWords(char *s)

Do it without using a second array.

Requirements

- You can use strlen(), strcmp(), and strncpy() if you wish, but you may not use any of the other C-string library functions such as strstr(), strncat(), etc.

- You have to use C-Strings (null-terminated arrays of chars) for this assignment.

- Need to write a simple main() function to call all of your functions and show that they work.

Solutions

Expert Solution

#include<iostream>
#include<cstring>

using namespace std;
//Declaration of Prototypes

int lastIndexOf(char *s, char);
void reverse(char *s);
int replace(char *s, char target, char replacementChar);
int findSubstring(char *s, char substring[]);
bool isPalindrome(char *s);
void WordReverser(char *s, int, int);
void reverseWords(char *s);
int main()
{
        const int MAX_SIZE = 300;
        int IndexOf;    

        char s[MAX_SIZE];
        char filler[2];
        char temp;
        char charTarget;
        cout << "Please enter in your string.\n";
        cin.getline(s, MAX_SIZE);
        cout << "\nNext, enter your target character.\n";
        cin >> charTarget;
        cin.getline(filler, 2);
        IndexOf = lastIndexOf(s, charTarget);
        cout << IndexOf << endl;


        cout << "Please enter a string you would like to reverse\n";
        cin.getline(s, MAX_SIZE);
        reverse(s);
        
        cout << "Please enter an array you'd like to replace a letter of.\n"
        cin.getline(s, MAX_SIZE);
        char letter, letter2;
        cout << "\nPlease enter a letter you would like to replace.\n";
        cin>> letter;
        cin.getline(filler, 2);
        cout << "\nPlease enter a letter you would like to place.\n";
        cin>> letter2;
        cin.getline(filler, 2);
        replace(s, letter, letter2);


        
        cout << "\nPlease enter a new string, with a substring you'd like to find.\n";
        cin.getline(s, MAX_SIZE);
        cout << "\nPlease enter a substring which you would like to find.\n";
        char substring[30];
        cin.getline(substring, 30);
        int place;
        place = findSubstring(s, substring);
        cout << "Your substring starts at "<<  place << endl;
        
        cout << "Please enter a string that might be a Palindrome.\n";
        cin.getline(s, MAX_SIZE);
        if(isPalindrome(s))
                cout<< "It's a palindrome!\n";
        else
                cout<<"It's not a palindrome...\n";
        
        cout<< "Please enter a string whose words you would like to reverse.\n";
        cin.getline(s, MAX_SIZE);
        reverseWords(s);
        cout << s;

        
        
                
return 0;
}


int lastIndexOf(char *s, char target)
{
        int counter = 0;
        for (int i = 0; i < strlen(s); ++i)
        {
                if (s[i] == target)
                        counter = i;
                
        }
        if (counter == 0)
                        return -1;
        return counter;
}

void reverse(char *s)
{
        int temp;
        char temp2;
        temp = (strlen(s) - 1);
        for (int i = 0; i < (strlen(s) / 2.0); ++i)
        {
                temp2 = s[i];
                s[i] = s[temp];
                s[temp] = temp2;
                temp--;
        }cout << s << endl;
}

int replace(char *s, char target, char replacementChar)
{
        int counter;
        for (int i = 0; i < strlen(s); ++i)
        {
                if (s[i] == target)
                        s[i] = replacementChar;
                counter++;
        }
        cout<< s;
        return counter;
}

int findSubstring(char *s, char substring[])
{
        int counter = 0;
        int temp;
        int j = 0;
        for (int i = 0; i < strlen(s); ++i)
        {
                if(s[i] == substring[j])
                {
                        for (j = 0; j < strlen(substring); ++j)
                        {
                                cout << s[i+j] << "   " << substring[j] << endl;
                                if (s[i + j] == substring[j])
                                        {
                                        counter++;
                                        cout << counter << endl;
                                        temp = i;
                                        }                       
                                if (counter == strlen(substring))
                                        return i;
                        }
                }       
        }
return -1;
}
        

bool isPalindrome(char *s)
{

        int temp = strlen(s) - 1;
        int counter = 0;
        for (int i = 0; i < (strlen(s)); ++i)
        {       if (s[i] == s[temp])
                {
                        counter++;
                }
        temp--;
        }
        if (counter == strlen(s))
                return true;
        else
                return false;
}

void reverseWords(char *s)
{
        reverse(s);
        for (int i = 0; i < strlen(s); ++i)
        {       
                for (int j = 0; j < strlen(s) +1; ++j)
                {
                        if (s[j] == ' ' || s[j] == '\0')
                        {WordReverser(s, i, j);
                                i = j + 1;
                        }
                }
        }

}

void WordReverser(char *s, int i, int j)
{
        int temp;
        char temp2;
        temp = j;
        
        for (; i < j; ++i)
        {
                temp2 = s[j];
                s[j] = s[i];
                s[i] = temp2;
                j--;
        }
}

Related Solutions

A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
Implement the following functions. Each function deals with null terminated C-strings. You can assume that any...
Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function returns 2. int...
For this program you will implement the following utility functions to test mastery of C strings....
For this program you will implement the following utility functions to test mastery of C strings. *******you MUST use these these function***** void removeBlanks(char *src, char *dest); void replaceChar(char *src, char oldChar, char newChar); char *flipCase(const char *src); Please read the description of these functions carefully. In the removeBlanks function you will implement a routine that takes a string in as src and outputs the same string into dest but removing any blank space character encountered. For example, if the...
Q) You have been asked to develop a program which processes C++ null-terminated strings to extract...
Q) You have been asked to develop a program which processes C++ null-terminated strings to extract character and numerical data separately. Assume that the users runs the application (examTest.exe) from the command line as follows: examTest.exe X-Axis:10,Y-Axis:17,Z-Axis:-6 Your application should step through the input arguments and extract the values of the XAxis, Y-Axis and Z-Axis commands. The user must always enter the arguments in the specified order, however your application should check they are correct. At the end of your...
PLEASE PROVIDE COMMENTS ON STEPS Write a C++ program that modifies a string (null terminated) as...
PLEASE PROVIDE COMMENTS ON STEPS Write a C++ program that modifies a string (null terminated) as follows: Consonants are positioned at the beginning of the string and vowels are moved to the end of the string. Example : Original string : washer New string : wshrae Note: The library string functions cannot be used. You must use pointers and the switch statement to execute this program. Assume that the vowels are a, e, i, o, and u. The modification has...
Selection Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user...
Selection Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user and gets at least 5 whole numbers as user input from the keyboard and stores them in an array Displays the numbers from the array on the screen Sorts the numbers in the array using SELECTION SORT Algorithm Displays the sorted numbers on the screen from the array Save your code file as "yourLastname_Firstname_SelectionSort.cpp" and submit your .cpp file. NOTE: This assignment needs only...
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user...
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user and gets at least 5 whole numbers as user input from the keyboard and stores them in an array Displays the numbers from the array on the screen Sorts the numbers in the array using BUBBLE SORT Algorithm Displays the sorted numbers on the screen from the array Save your code file as "yourLastname_Firstname_BubbleSort.cpp" and submit your .cpp file. NOTE: This assignment needs only...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT