Solved

Borland C++ String question

Posted on 1997-09-21
4
232 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
Comment Utility
#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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks a lot

chris
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
multiple DLLs with the same namespace 5 83
Quicksort a dynamic deque 33 57
C++ Language error 28 143
Compile GLUT with Visual Studio 2015 1 81
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 clear a vector as well as how to detect empty vectors in C++.

762 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

10 Experts available now in Live!

Get 1:1 Help Now