?
Solved

Borland C++ String question

Posted on 1997-09-21
4
Medium Priority
?
279 Views
Last Modified: 2008-03-06
I need to create a function called MyStringFunct which accept 2 built-in strings(character arrays terminated by '\0') as parameters and returns an integer.

int MyStringFunct( char * parm1, char * parm2 )

The program should process according to the following:

Parm1          Parm2         Processing               Return value
"Initialize"   "Any Text"  Store Parm2 in a static              -1
                                      variable local to
                                      MyStringFunct

"Vowels"     NULL                                          No. of vowels
                                                                       in the static
                                                                       variable.

"Consonants" NULL                                      No. of conso-
                                                                      nents in the
                                                                      static variable.

Note: Be sure the function should work no matter what order the above calls are made.

More: How to change the parm1 to be a pointer to a function. Implement the same actions using different functions for "Init", "Vowel" etc..

Could you please write me some code so I can work on this exercise in my book?

Thanks
Chris Lee            
0
Comment
Question by:ny971181
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
yonat earned 40 total points
ID: 1170158
#include <string.h>
#include <ctype.h>

#define MAX_TEXT_LEN 100

int Initialize(char* text, char* content);  /* copy content into text */
int Vowles(char* text);                     /* number of vowels in text */
int Consonants(char* text);                 /* number of consonants in text */

int MyStringFunct( char* param1, char* param2)
{
    static char text[MAX_TEXT_LEN] = "";

    if (!strcmp(param1, "Initialize"))
        return Initialize(text, param2);
    else if (!strcmp(param1, "Vowels"))
        return Vowles(text);
    else if (!strcmp(param1, "Consonants"))
        return Consonants(text);
   
    return -1; // error in input
}

int Initialize(char* text, char* content)
{
    strcpy(text, content);
    return -1;
}

int IsVowel(int c)
{
    c = tolower(c);
    return ( c == 'a' || c == 'e' || c == 'i'
             || c == 'o' || c == 'u');
}

int Vowles(char* text)
{
    int count = 0;

    while (*text != '\0') {
        if (IsVowel(*text))
            ++count;
        ++text;
    }

    return count;
}

int Consonants(char* text)
{
    int count = 0;

    while (*text != '\0') {
        if (isalpha(*text) && !IsVowel(*text))
            ++count;
        ++text;
    }

    return count;
}

0
 
LVL 1

Author Comment

by:ny971181
ID: 1170159
It looks fine but is it true that it has to have a main() in a program?

Chris Lee

Urgent!

0
 
LVL 5

Expert Comment

by:yonat
ID: 1170160
int main()
{
    char buf[MAX_TEXT_LEN];

    for(;;) {
        cout << "Enter string to analyze\n";
        cin >> buf;
        cout << '\n';
        MyStringFunct("Initialize", buf);
        cout << "The number of vowels is " <<
            MyStringFunct("Vowels", NULL) <<
            ", and the number of consonants is " <<
            MyStringFunct("Consonants", NULL) << ".\n";
    }

    return 0;
}

0
 
LVL 1

Author Comment

by:ny971181
ID: 1170161
thanks a lot

chris
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question