In: Computer Science
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;
}
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: