Question

Write a function oneSpaceAfterPeriod( String Parameter ) that will return a string with one space after...

Write a function oneSpaceAfterPeriod( String Parameter ) that will return a string with one space after every period. Another function twoSpaceAfterPeriod (String Parameter) that will return a string with two spaces after every period.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ONE SPACE METHOD

//method to provide one space after period symbol
string oneSpaceAfterPeriod(string s)
{
    int i,j=0;
    char s1[s.size()+1]; //allocate memory for a character array according to size of s
    for(i=0;i<s.length();i++)
    {
        if(s[i]=='.') //if period symbol found
        {
            s1[j]='.'; //copy the period symbol to converted string
            j++; //j is used for the index of converted string
            s1[j]=' '; //assign a white space to the string
            j++;
           }
           else
           { //copy all charcters except period symbol
               s1[j]=s[i];
               j++;
           }
             
   }
   s1[j]='\0'; //assign null to end
     
   return s1; //return converted string
}

TWO SPACE METHOD

//method to provide two white spaces after period symbol
string twoSpaceAfterPeriod(string s)
{
    int i,j=0;
    char s1[s.size()+1];
    for(i=0;i<s.length();i++)
    {
        if(s[i]=='.')//if period symbol found
        {
            s1[j]='.'; //copy the period symbol to converted string
            j++;//j is used for the index of converted string
            s1[j]=' '; //assign a white space after period symbol
            j++;
            s1[j]=' ';//assign second white space
            j++;
           }
           else//copy all charcters except period symbol
           {
               s1[j]=s[i];
               j++;
           }
             
   }
   s1[j]='\0'; //assign null to end
     
   return s1; //return converted string
}

PROGRAM IMPLEMENTATIONS

#include<iostream>
using namespace std;
//method to provide one space after period symbol
string oneSpaceAfterPeriod(string s)
{
    int i,j=0;
    char s1[s.size()+1]; //allocate memory for a character array according to size of s
    for(i=0;i<s.length();i++)
    {
        if(s[i]=='.') //if period symbol found
        {
            s1[j]='.'; //copy the period symbol to converted string
            j++; //j is used for the index of converted string
            s1[j]=' '; //assign a white space to the string
            j++;
           }
           else
           { //copy all charcters except period symbol
               s1[j]=s[i];
               j++;
           }
             
   }
   s1[j]='\0'; //assign null to end
     
   return s1; //return converted string
}
//method to provide two white spaces after period symbol
string twoSpaceAfterPeriod(string s)
{
    int i,j=0;
    char s1[s.size()+1];
    for(i=0;i<s.length();i++)
    {
        if(s[i]=='.')//if period symbol found
        {
            s1[j]='.'; //copy the period symbol to converted string
            j++;//j is used for the index of converted string
            s1[j]=' '; //assign a white space after period symbol
            j++;
            s1[j]=' ';//assign second white space
            j++;
           }
           else//copy all charcters except period symbol
           {
               s1[j]=s[i];
               j++;
           }
             
   }
   s1[j]='\0'; //assign null to end
     
   return s1; //return converted string
}

//driver program
int main()
{
   string s,s1,s2;
   cout<<endl<<"Enter a string";
   getline(cin,s);//read the string
   //display the string
   cout<<endl<<"Entered String is : "<<s;
    s1=oneSpaceAfterPeriod(s); //call method to provide one space
        //display the converted string
   cout<<endl<<"Converted String with one space after period is : "<<s1;
   s2=twoSpaceAfterPeriod(s);//call method to provide two spaces
   //display the converted string
   cout<<endl<<"Converted String with two spaces after period is : "<<s2;
  
}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Write a function oneSpaceAfterPeriod( String Parameter ) that will return a string with one space after...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT