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

Viewed 62

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] != '\0'; 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

3 Answers

Since you use C++ and not C, I suggest you use string instead of char[] as it may be easier to handle sentences and do operations with them this way, particularly if you come from a higher-level language, or if you are new altogether.

If you wanna print the first letter of each word in the string, you can iterate through the string like this:

void printFirstLetter(string sentence) { 
    int i; 
  
    for (i = 0; i < sentence.length(); i++)  
    { 
        //Print the first letter
        if (i == 0) 
            cout<<sentence[i]; 
  
        // Find the space separating the words, then print the next first letter again.
        if (sentence[i] == ' ')  
        { 
            cout<<sentence[i-1]<<" "<<sentence[i+1]; 
        } 
    } 
} 

By iterating through the string, for example, if you have "George Likes Cats" you will get: G L C.

Since you use C++ and not C, you can use string instead of chars. This may help fix some issues if you're new to programming.

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.

You can try this out by using Modern C++.

#include <iostream>
#include <sstream>
#include <string>

const std::string findAbbrivation(const std::string& sentance){
  std::istringstream ss(sentance);
  std::string word;
  std::string result;
  while(std::getline(ss, word, ' '))  {
      result += std::toupper(word[0]);
  }
  return result;
}


int main(){
    std::cout << findAbbrivation("Pakistan State Oil") << std::endl;
}

The idea is that you split your string by spaces and then convert the first letter toupper and then concatenate it to the result string. You can read about getline here.

Related