Challenging Puzzle: Need help coding program to manipulate char string

I need help writing the last two required functions of this program: 1st i've posted the the instructions given to me. 2nd is the code I have thus far. Please help me Thank you very much you have no idea how much this helps me out.





Miss String wants you to help her develop a program that has two string functions. The program that she has in mind will display a menu of two string functions: function one will substitute a specified character with a given character and function two will count a specified character.

Declare an array of 80 characters in main(). Also, prompt the user for the string in main() and store the string into the array of characters that you have declared.

Your main() program will display a menu by calling Function 1, based on the menu choice returned by Function 1, call the appropriate function based on user’s selection. For each function 2 and 3, prompt for user’s input in main() and pass those information as arguments into the function.

Your program will allow Miss String to perform as many string operations as she wishes.
For the loop that controls the iteration, use the following condition: C for Continue and any other character to stop. For each user input, first check to see if the input is a lower case character (hint: use one of the standard string functions), if it is, call a standard string function to convert it to upper case before using it. This does not apply to character input for function 2 and 3 but it does apply to function 1.

You are to write four functions:
Function 1: (No argument, return a character value)
This function will display the menu choice: A – Substitute Character, B – Count Character. Call this function at the beginning of your program to display the menu. In the function, also, prompt for user’s choice, check for invalid input and loop until correct input is received. Return the choice back to main().

Function 2: (Accept three arguments: an array and two characters, and return no value)
This function will accept an array containing the string and two characters. It will scan for the first character and when it finds the character, it will substitute the first character with the second character. Prompt for the character to be scanned and the character to be substituted in main() and pass those into the function.

Function 3: (Accept two arguments: a pointer to the beginning of the string array and a character, return an integer value)
This function will accept a character pointer and a character. It will use the pointer to scan for the specified character. When it finds the specified character, it will start counting the number of times the specified character appears. It will also place a character 1 or 2 or 3 and so on into the place of the specified character in the string. For example, if ‘i’ is the specified character, the first occurrence of ‘i’ will be replaced with 1 and the second occurrence of ‘i’ will be replaced with 2. Because the string is stored in an array of characters, you can not place integer number into the string.



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
#include <cstdlib>
#include <iomanip>
#include <iostream>

using namespace std;

char Func_menu(); //Menu Function prototype //




int main()
{
    char menu_choice;         //user input from menu function
    char oldchar;             //entry for character to be scanned
    char newchar;             //user entry to replace old character
    char string_array[80];    //array to hold string characters
    
    
      cout << "\n\n\n\nWelcome Ms. String's String Editor!"
              "\n---------------------------"
              "\nPlease Input A String:\n";
      cin >> string_array;
    
    Func_menu();              //calls menu function to display user menu
    menu_choice = Func_menu();        //sets menuchoice to entry gathered from user
    
    switch(menu_choice)               // in menu function
    {
         case 'a':
         case 'A':
              cout << "\n\n\n\nSubstitute Character"     //asks user for substitution entries
              "\n---------------------------"
              "\nPlease Enter The Character To Be Scanned:";
              cin >> oldchar;
              
              cout << "\nPlease Enter The Character To Be Substituted:";
              cin >> newchar;
              
                            
              break;
         case 'b':
         case 'B':
              cout << "\n\nCharacter Counter"       //asks user which character to count
              
              
              
              break;
    }
    system("PAUSE");
    return 0;
}


char Func_menu()    //Definition header
{   
      char selection;
      
      cout << "\nString Editor"
              "\n---------------------------"
              "\nPlease Select from the Options Below:\n"
              "\n---------------------------"
              "\n(A)Substitute Character"
              "\n(B)Count Character"
              "\nPlease Make a Selection Using Letters in Parenthesis:\n\n";
      cin >> selection;
      
      switch(selection)   //switch loop to check for valid entry, loop until valid entry recieved
      {
           case 'a': 
           case 'A':
                return selection;
                break;
                
          case 'b':  
          case 'B':
               return selection;
               break;
               
          default:   //INVALID ENTRIES ON MAIN MENU
                    cout << "\nInvalid Entry, Please Try Again\n";
                     Func_menu();
                    break;
          }
     
}



here is also an example of what the output should look like on the console screen:

Sample Output:


Please enter a string: Tvinkle tvinkle little star


Welcome to the string program !
===============================
A - Substitute Character
B - Count Character

Enter your choice: p

Invalid selection ! Please re-enter !

Welcome to the string program !
===============================
A - Substitute Character
B - Count Character

Enter your choice: a

Please enter the character to be scanned: v


Please enter the character to be substituted: w


The updated string is : Twinkle twinkle little star

Do you want to continue? C


Please enter a string: How are you doing today?


Welcome to the string program !
===============================
A - Substitute Character
B - Count Character

Enter your choice: B

Please enter the character to be scanned: o


The updated string is: H1w are y2u d3ing t4day?

Character o appears in the string 4 times

Do you want to continue? Z


Press any key to continue
Last edited on
function2 should look something like this:

1
2
3
void swapChars(char* str, char c1, char c2) {
  for (int i=0;i<80;i++) if (str[i]==c1) str[i]=c2;
}


function 3 should look something like this:
1
2
3
4
5
6
7
8
9
int countChar(char* str, char c) {
  int count=0;
  for (int i=0;i<80;i++)
  if (str[i]==c) { 
    count++;
    if (count<=9) str[i]=count+48;
  }
  return count;
}


Note that since this is an array of characters, we can only insert a single digit number into the array in place of the character it counts. The maximum times we can do this is 9 times. The program keeps counting the character through the 80 elements of the array, though. Also, to convert an integer number between 0 and 9 to it's corresponding character representation, we need to add 48 to get the proper ASCII code. Otherwise, what you get for number 1 is ASCII character 1 which is a smiley face, not the character '1'. This is the reason for that addition statement.

~psault



Last edited on
Thanks, one more question tho, how do you call the function from main()?? I can't seem to get the syntax correct, keep getting error messages.

ex. Swap_chars(string_array*, oldchar, newchar);

error message: expected primary-expression before ',' token
Try removing the * (but only from your call, not the declaration & header for the function).


~psault
Last edited on
Topic archived. No new replies allowed.