Solved

Borland C++ String question

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

739 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