Solved

Borland C++ String question

Posted on 1997-09-21
4
275 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 10 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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

717 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