Part 1: MyStringFunctions Set up an empty, console C++ project called MyStringFu
ID: 3552181 • Letter: P
Question
Part 1: MyStringFunctions
Set up an empty, console C++ project called MyStringFunctions. Then, add a
Source File to the project named MyStringFunctions.cpp.
Implement all of the functions described below. Each function should be called
and tested from main many times to prove to your self that it works correctly.
a. A myErase function that takes in an integer and a string. It should erase
the letter from the string at the given index and then return the new,
smaller string.
If the index is bigger than the length of the string the function returns the
string unchanged. Do not use the erase member function of the string
class.
b. A mySubstring function. This function takes two strings and if the first
string is a substring of the second string it returns an index indicating
where the substring starts. Otherwise it returns --??1. Do not use the substr
member function of the string class.
c. The C++ standard library function int strlen(const char *s) Determines the
length of a string: the number of characters preceding the terminating null
character is returned. Write your own strlen function in two different
ways as described below.
(The first version should use array subscripts and subscript arithmetic, andthe second version should use pointers and pointer arithmetic.)
d. There is an encoded language called UpOne in which each letter in the
word is changed to the next letter in the alphabet: '?a'? to '?b'?, '?b'? to '?c'?, and so
on. The code is wrapped around at the end of the alphabet, so that '?z'?
maps to '?a'?. Write a function called translate that maps a single word into
the corresponding word in the UpOne code. You may assume the word
contains only lower--??case letters. An example usage is shown below:
cout << translate("abcd"); //Prints: bcde
string s = "zamboni";
cout << translate(s); //Prints: abncpoj
e. A palindrome is a word that is the same backwards and forwards. For
example, the strings below are all palindromes:
ODO
dood
racecar
H*q*H
123321
Write a bool function isPalindrome that returns true if a given string is a
palindrome and false if the string is not a palindrome.
Here is my code for the a. I need help for another part(please finish it based on this code below):
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespacestd;
char* myErase(char *word ,int index);
const int size= 100;
int main()
{
int index;
charstr[size];
cout << "Please enter the word : ";
cin.getline(str,size);
cout << "Please which letter you want to delete: ";
cin >> index;
cout<< myErase(str,index) << endl;
cin.ignore();
cin.get();
return 0;
}
char* myErase(char *str ,int index)
{
if(index>strlen(str)){
return str;
}
for(int i=index;i<strlen(str)-1;i++)
{
*(str+i)=*(str+i+1);
}
*(str+strlen(str)-1)='';
return str;
}
Explanation / Answer
// MyStringFunctions.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<string>
#include<iostream>
using namespace std;
string myErase(unsigned int position,string str)
{
string newstr;
if(position>=str.length())
return str;
else
{
for(int i=0;i<str.length();i++)
{
if(i==position)
continue;
newstr.push_back(str[i]);
}
}
return newstr;
}
int mySubstring(string sub,string super)
{
if(sub.length()>super.length())
return -1;
else
{
for(int i=0;i<super.length()-sub.length()+1;i++)
{
int j,k;
for(j=0,k=i;j<sub.length();j++,k++)
{
if(sub[j]!=super[k])
break;
}
if(j==sub.length())
return i;
}
}
return -1;
}
//using array subscript
int strlenArray(const char s[])
{
int i=0;
while(s[i]!='')
{
i++;
}
return i;
}
//using pointer airthmatic
int strlenPointer(const char *s)
{
int i=0;
while(*(s+i)!='')
i++;
return i;
}
string UpOne(string str)
{
string newstr;
for(int i=0;i<str.length();i++)
{
if(str[i]=='z')
newstr.push_back('a');
else
newstr.push_back(str[i]+1);
}
return newstr;
}
bool isPalindrome(string str)
{
for(int i=0;i<str.length()/2;i++)
{
if(str[i]!=str[str.length()-i-1])
return false;
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
string str;
str="abcdefgh";
char a[]="string";
cout<<str<<endl;
cout<<myErase(4,str)<<endl;
cout<<mySubstring("cde",str)<<endl;
cout<<strlenArray(a)<<endl;
cout<<strlenPointer(a)<<endl;
cout<<UpOne(str)<<endl;
cout<<isPalindrome("abba")<<endl;
system("PAUSE");
return 0;
}