Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include using namespace std; int stringLength (const char* s) //returns the num

ID: 3917218 • Letter: #

Question

#include using namespace std; int stringLength (const char* s) //returns the number of printable characters in s int countChars (const chan s, const char ch) //returns the number of tines the character ch is found in s int findchar(const chan* s, const char ch, const int startIndex, const int lastIndex) returns the first index where the character ch is found in s starting from startIndex (inclusive) upto lastIndex (exclusive) If ch is not found in s in the interval, it returns -1 This function must first validate both the startIndex and lastIndex That is, if lastIndex stringLength(s) or startIndex 8 it must return-1 void rotateString(char s, const int r) Rotate the characters of s by r units If re, rotate the characters of s to the left If r

Explanation / Answer

Here is the solution to the question asked, you can write the main functio yourself and test it.

#include<iostream>

using namespace std;

int stringLength(const char* s)

{

int i;

for(i=0;s[i]!='';i++);

return i;

}

int countChars(const char* s, const char ch)

{

int count = 0;

for(int i=0;s[i]!='';i++)

{

if(s[i]==ch)

count++;

}

return count;

}

int findChar(const char* s ,const char ch, const int startIndex, const int lastIndex)

{

int len=stringLength(s);

if(lastIndex>len || startIndex<0)

return -1;

for(int i=startIndex;i<lastIndex;i++)

{

if(s[i]==ch)

return i;

}

return -1;

}

void rotateString(char* s, const int r)

{

/*This works on the string reversal algorithm, we first reverse the first r characters,then the remaing characters

and then the complete string, which gives us the desired result*/

/* Try this on paper and you will get understand better */

int len=stringLength(s);

int rot=r%len;

if(rot<0)

rot=rot+len;

int left,right;

left=0;

right=rot-1;

while(left<right)

{

int t=s[left];

s[left]=s[right];

s[right]=t;

left++;

right--;

}

left=rot;

right=len-1;

while(left<right)

{

int t=s[left];

s[left]=s[right];

s[right]=t;

left++;

right--;

}

left=0;

right=len-1;

while(left<right)

{

int t=s[left];

s[left]=s[right];

s[right]=t;

left++;

right--;

}

}

void append(char*& s,const char ch)

{

int len=stringLength(s);

s[len]=ch;

s[len+1]='';

}

void append(char*& s1, const char* s2)

{

int l1=stringLength(s1);

int l2=stringLength(s2);

int i;

for(i=0;i<l2;i++)

s1[l1+i]=s2[i];

s1[l1+i]='';

}

void removeAll(char*& s,const char ch)

{

int len=stringLength(s);

int j=0;

for(int i=0;i<len;i++)

{

if(s[i]!=ch)

s[j++]=s[i];

}

s[j]='';

}

char* zigzagMerge(const char*s1, const char* s2)

{

int l1=stringLength(s1);

int l2=stringLength(s2);

int len=l1+l2;

char* ans=new char[len];

int i,j,k;

i=j=k=0;

while(i<l1 && j<l2)

{

ans[k++]=s1[i++];

ans[k++]=s2[j++];

}

while(i<l1)

ans[k++]=s1[i++];

while(j<l2)

ans[k++]=s2[j++];

return ans;

}

bool isAnagram(const char *s1, const char* s2)

{

/* count array counts the occurences of each character in the array*/

int count[256]={0};

/* It increments for each occurence in the first array and decrements for the second array*/

int i;

for(i=0;s1[i] && s2[i];i++)

{

count[s1[i]]++;

count[s2[i]]--;

}

/* This condition checks if the 2 strings are of different length*/

if(s1[i] || s2[i])

return false;

for(int i=0;i<256;i++)

if(count[i])

return false;

return true;

}

bool isSubString(const char* s1, const char* s2)

{

int l1=stringLength(s1);

int l2=stringLength(s2);

for(int i=0;i<=l2-l1;i++)

{

int j;

for(j=0;j<l1;j++)

if(s2[i+j]!=s1[j])

break;

if(j==l1)

return true;

}

return false;

}

int countWords(const char*s)

{

int len=stringLength(s);

int i=0;

int ans=0;

while(i<len)

{

while(i<len && s[i]!=' ')

i++;

ans++;

while(i<len && s[i]==' ')

i++;

}

return ans;

}