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...
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....
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 <<...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must be very basic. Please don't use math sqrt. Assume that the user does not input anything less than 0. For example: the integer square root of 16 is 4 because 4 squared is 16. The integer square root of 18 is 5 because 4 squared is 16 and 5 squared is 25, so 18 is bigger than 16 but less than 25.  
Hi, i need flowchart for this code (C++) please, THANX #include <iostream> #include <thread> #include <unistd.h>...
Hi, i need flowchart for this code (C++) please, THANX #include <iostream> #include <thread> #include <unistd.h> #include <semaphore.h> #include <pthread.h> using namespace std; #define NRO 6 // Número de coches //Puente declarado con matriz y valor entero void Puente(string array, int value); // Variable global int Norte = 1; int Sur = 1; sem_t mutex1; //Coche al norte void* NorteC(void* arg){ sem_wait(&mutex1); string array = "En el lado Norte "; // Norte cout<<array<<"del puente, el coche #"<<Norte<<" puede cruzar el...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT