Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

c++ - I want to Print the Abbrevation of the Word but dont know what I am doing wrong

I want to Print the Abbreviation of the Word Write a C program that should take an organization’s name from the user as a string. You have to implement the following function on that string:
For example:
Word = Pakistan State Oil
Abbreviation: PSO Only Capital Letters from the Word

It is Printing only 1 word.

Code :

#include <iostream>
using namespace std;

void printAbbrevation(char Word[], char WordAbb[]);
int findLength(char Word[])
{
    int length = 0;

    for (int i = 0; Word[i] != ''; i++)
    {
        length++;
    }
    return length;
}

void findAbbrevation(char Word[])
{
    int WordLength = findLength(Word);
    char WordAbb[] = {0};
    
    for (int i = 0; i < WordLength; i++)
    {
        if (Word[i] >= 'A' && Word[i] <= 'Z')
        {
            WordAbb[i] = Word[i];
        }
    }
    printAbbrevation(Word, WordAbb);
}

void printAbbrevation(char Word[], char WordAbb[])
{
    int abbword = findLength(WordAbb);
    cout << "Abbrevation of " << Word << " is = ";

    for (int i = 0; i < abbword; i++)
    {
        cout << WordAbb[i];
    }
    
}

int main()
{
    char Word[100] = {0};
    cout << "Enter the Word = ";
    cin.getline(Word,100);
    cout << "Length of Word is = ";
    cout << findLength(Word);
    cout << endl;
    findAbbrevation(Word);
    return 0;
}

Output I am getting :
Output

question from:https://stackoverflow.com/questions/65517035/i-want-to-print-the-abbrevation-of-the-word-but-dont-know-what-i-am-doing-wrong

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to change your findAbbrevation() function like this:

void findAbbrevation(char Word[])
{
    int WordLength = findLength(Word);
    char WordAbb[25] = {0};
    int j = 0;
    
    int flag = 0;
    for (int i = 0; i < WordLength; i++)
    {
        // To continue untill we get a space
        // this marks the start of the new word
        if(flag)
        {
            if(Word[i] == ' ')
            {
                flag = 0;
            }
            continue;
        }
        else
        {
            if (Word[i] >= 'A' && Word[i] <= 'Z')
            {
                WordAbb[j++] = Word[i];
                flag = 1;
            }
        }
    }
    printAbbrevation(Word, WordAbb);
}

Changes I have made are:

  • Keep a flag when you find the new word, which is done by checking for spaces in the string.
  • For each new word found, add it's first character to the abbreviation string.
  • Keep a separate counter for the abbreviation string.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...