Solved

Borland C++ String question

Posted on 1997-09-21
4
241 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now