Question

In: Computer Science

A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using...

A C++ question:

I want to indent the code of this C++ program:

#include<iostream>
#include<cstring>
using namespace std;

int lastIndexOf(char *s, char target)
{
int n=strlen(s);
for(int i=n-1;i>=0;i--)
{

if(s[i]==target)
{
return i;
}
}
return -1;
}

void reverse(char *s)
{
int n=strlen(s);
int i=0,j=n-1;
while(i<=j)
{
char temp=s[i];
s[i]=s[j];
s[j]=temp;
i++;
j--;
}
return;
}

int replace(char *s, char target, char replacementChar)
{
int len=strlen(s);
int total=0;
for(int i=0;i<len;i++)
{
if(s[i]==target)
{
s[i]=replacementChar;
total+=1;
}
}
return total;
}

int findSubstring(char *s, char substring[])
{
int len1=strlen(s);
int len2=strlen(substring);
for(int i=0;i<len1-len2+1;i++)
{
bool found=true;
for(int j=i;j<i+len2;j++)
{
if(s[j]==substring[j-i])
{
}
else
{
found=false;
break;
}
}

if(found==true)
{
return i;
}
}
return -1;
}
bool isPalindrome(char *s)
{
int n=strlen(s);
int i=0,j=n-1;
while(i<=j)
{
if(s[i]==s[j])
{
i++;
j--;
}
else
{
return false;
}
}
return true;
}

void reverseWords(char *s)
{
int len=strlen(s);
int j=0;
for(int i=0;i<len;i++)
{
if(s[i]==' ')
{
int p1=j,p2=i-1;
while(p1<=p2)
{
char temp=s[p1];
s[p1]=s[p2];
s[p2]=temp;
p1++;
p2--;
}
j=i+1;
}
}
{
int p1=j,p2=len-1;
while(p1<=p2)
{
char temp=s[p1];
s[p1]=s[p2];
s[p2]=temp;
p1++;
p2--;
}
}
reverse(s);
return;
}

int main()
{
int len;
cin>>len;
cin.ignore();
char*s=new char[len];
scanf("%[^\n]%*c", s);
char ch1='a',ch2='A';
int in=lastIndexOf(s,'a');
if(in==-1)
{
cout<<ch1<<" not found in"<<s<<endl;
}
else
{
cout<<ch1<<" found at "<<in<<" in "<<s<<endl;
}
cout<<endl;
reverse(s);
cout<<"reversed string is: "<<s<<endl;
cout<<endl;
int count=replace(s,'a','A');
cout<<"Total number of replacements for "<<ch1<<" to "<<ch2<<" are "<<count<<endl;
cout<<endl;

char substring[]="abc";

int start=findSubstring(s,substring);
if(start==-1)
{
cout<<substring<<" not found in "<<s<<endl;
}
else
{
cout<<substring<<" found at "<<start<<" in "<<s<<endl;
}
cout<<endl;
bool check=isPalindrome(s);
if(check==true)
{
cout<<s<<" is a palindrome."<<endl;
}
else
{
cout<<s<<" is not a palindrome."<<endl;
}
cout<<endl;
cout<<"Before reversing words: "<<s<<endl;
reverseWords(s);
cout<<"After reversing words: "<<s<<endl;

return 0;
}

Solutions

Expert Solution

The indented version of the given code and the corresponding output are as follows:

#include<iostream>
#include<cstring>
using namespace std;

int lastIndexOf(char *s, char target)
{
        int n=strlen(s);
        for(int i=n-1;i>=0;i--)
        {       
                if(s[i]==target)
                {
                  return i;
                }
        }
        return -1;
}

void reverse(char *s)
{
        int n=strlen(s);
        int i=0,j=n-1;
        while(i<=j)
        {
                char temp=s[i];
                s[i]=s[j];
                s[j]=temp;
                i++;
                j--;
        }
        return;
}

int replace(char *s, char target, char replacementChar)
{
        int len=strlen(s);
        int total=0;
        for(int i=0;i<len;i++)
        {
                if(s[i]==target)
                {
                        s[i]=replacementChar;
                        total+=1;
                }
        }
        return total;
}

int findSubstring(char *s, char substring[])
{
        int len1=strlen(s);
        int len2=strlen(substring);
        for(int i=0;i<len1-len2+1;i++)
        {
                bool found=true;
                for(int j=i;j<i+len2;j++)
                {
                        if(s[j]==substring[j-i])
                        {
                        }
                        else
                        {
                                found=false;
                                break;
                        }
                }
                
                if(found==true)
                {
                        return i;
                }
        }
        return -1;
}

bool isPalindrome(char *s)
{
        int n=strlen(s);
        int i=0,j=n-1;
        while(i<=j)
        {
                if(s[i]==s[j])
                {
                        i++;
                        j--;
                }
                else
                {
                        return false;
                }
        }
        return true;
}

void reverseWords(char *s)
{
        int len=strlen(s);
        int j=0;
        for(int i=0;i<len;i++)
        {
                if(s[i]==' ')
                {
                        int p1=j,p2=i-1;
                        while(p1<=p2)
                        {
                                char temp=s[p1];
                                s[p1]=s[p2];
                                s[p2]=temp;
                                p1++;
                                p2--;
                        }
                        j=i+1;
                }
        }
        {
        int p1=j,p2=len-1;
        while(p1<=p2)
        {
                char temp=s[p1];
                s[p1]=s[p2];
                s[p2]=temp;
                p1++;
                p2--;
        }
        }
        reverse(s);
        return;
}

int main()
{
        int len;
        cin>>len;
        cin.ignore();
        char*s=new char[len];
        scanf("%[^\n]%*c", s);
        char ch1='a',ch2='A';
        int in=lastIndexOf(s,'a');
        if(in==-1)
        {
                cout<<ch1<<" not found in "<<s<<endl;
        }
        else
        {
                cout<<ch1<<" found at "<<in<<" in "<<s<<endl;
        }
        cout<<endl;
        reverse(s);
        cout<<"reversed string is: "<<s<<endl;
        cout<<endl;
        int count=replace(s,'a','A');
        cout<<"Total number of replacements for "<<ch1<<" to "<<ch2<<" are "<<count<<endl;
        cout<<endl;
        
        char substring[]="abc";
        
        int start=findSubstring(s,substring);
        if(start==-1)
        {
                cout<<substring<<" not found in "<<s<<endl;
        }
        else
        {
                cout<<substring<<" found at "<<start<<" in "<<s<<endl;
        }
        cout<<endl;
        bool check=isPalindrome(s);
        if(check==true)
        {
                cout<<s<<" is a palindrome."<<endl;
        }
        else
        {
                cout<<s<<" is not a palindrome."<<endl;
        }
        cout<<endl;
        cout<<"Before reversing words: "<<s<<endl;
        reverseWords(s);
        cout<<"After reversing words: "<<s<<endl;
        
        return 0;
}

Output:


Related Solutions

I want to indent this c++ program #include<iostream> using namespace std; class Rectangle{ private: double width;...
I want to indent this c++ program #include<iostream> using namespace std; class Rectangle{ private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; double getPerimeter() const; bool isSquare() const; }; void Rectangle::setWidth(double w){ width = w; } void Rectangle::setLength(double l){ length = l; } double Rectangle::getWidth() const{ return width; } double Rectangle::getLength() const{ return length; } // Added Method definitions double Rectangle::getArea() const{ return (width * length); } double Rectangle::getPerimeter() const{...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat = 'y'; for (;repeat == 'y';){ char emplyeename[35]; float basic_Salary,EPF, Dearness_Allow, tax, Net_Salary , emplyee_id; cout << "Enter Basic Salary : "; cin >> basic_Salary; Dearness_Allow = 0.40 * basic_Salary; switch (01) {case 1: if (basic_Salary <= 2,20,00) EPF = 0; case 2: if (basic_Salary > 28000 && basic_Salary <= 60000) EPF = 0.08*basic_Salary; case 3: if (basic_Salary > 60000 && basic_Salary <= 200000)...
Please, write code in c++. Using iostream and cstring library. Your friend is the person who...
Please, write code in c++. Using iostream and cstring library. Your friend is the person who does not like any limitations in the life. And when you said to him that it is totally impossible to work with integer numbers bigger than 4 294 967 296 in C++ he blamed you in time-wasting during the university study.So to prove that you hadn't waste 2 months of your life studying C++ in university you have to solve this issue. Your task...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
Please, write code in c++. Using iostream and cstring library Use pointers! You given a text.Your...
Please, write code in c++. Using iostream and cstring library Use pointers! You given a text.Your task is to write a function that will find the longest sequence of digits inside. Note that the output have to be presened just like in sample. Note. The program have to use pointer. Input: First line contains one line that is not longer than 1000. Output: The longest sequence of numbers.All numbers are positive and integers. Samples: № INPUT OUTPUT 1 This is...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using namespace std; const int CWIDTH = 26; int main() {    int choice;    double convertFoC, converCtoF;    double starting, endvalue, incrementvalue;    const int CWIDTH = 13;    do {       cin >> choice;    switch (choice)    {        cin >> starting;    if (starting == 28){       cout << "Invalid range. Try again.";    }    while (!(cin >> starting)){       string  garbage;       cin.clear();       getline(cin, garbage);       cout << "Invalid data Type, must be a number....
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int SIZE = 20;     char str[SIZE];     char str1[SIZE];     int n;     int k =1;        printf("Enter a word: \n");     fgets(str,SIZE,stdin);     printf("Enter another word: \n");     fgets(str1,SIZE,stdin);        if (str1[strlen(str1) - 1] == '\n')     {         str1[strlen(str1)-1] = '\0';     }     if (str[strlen(str) - 1] == '\n')     {         str[strlen(str)-1] = '\0';     }      ...
I have a error code, for my C++ class, using Putty include <iostream> using namespace std;...
I have a error code, for my C++ class, using Putty include <iostream> using namespace std; int main() { char answer; char grade; cout << "Are you taking a class (enter y, Y, N or N): " << endl; cin >> answer; if (answer == 'N' || 'n'); { cout << "Thanks for using the system" << endl; } else if (answer == 'Y' || 'y'); { cout << "Enter a letter grade (A, B, C, D, or F): "...
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate...
C++ I took 7/20 =( code: #include <iostream> #include<string.h> using namespace std; // function to calculate number non white space characters int GetNumOfNonWSCharacters(string str) { int i = 0; int count = 0; while(str[i] != '\0') { if(str[i] != ' ') { count += 1; } i++; } return count; } // function to calculate numbers of words int GetNumOfWords(string str) { int i = 0; int count = 1; while(str[i] != '\0') { if(str[i] == ' ' && str[i-1]...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std;...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std; void leapYear(int x); int main() { int x; cout << "Enter a year: "; cin >> x; leapYear (x);   return 0; } void leapYear(int x ) {    if (x % 400 == 0)    {    cout << "This is a leap Year";}    else if    ((x % 4 == 0) && (x % 100 != 0))    {    cout <<...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT