hey guys, I was wondering if you can look over some code:
1. char* reverse(char *s);
*Here, s is a C-string. Its return value should be a newly-allocated C-string, said string being the reversal of the
string s. Hence, the return value of reverse("abcde") would be "edcba". *
Code:
{
char *t;
int i, n;
n=length(s);
t = new char[n];
for (i=0; i<n; ++i)
{
*(t+i)=*(s+n-1-i);
}
*(t+i)='\0';
return t;
}
}
2. char* trimfront(char *s);
*Here, s is a C-string. Its return value should be a newly-allocated C-string, consisting of s, but with all initial whitespace characters removed*
Code:
{
while(*s == ' ' || *s == '\t')
s++;
return s;
}
3. char* trimrear(char *s);
*Here, s is a C-string. Its return value should be a newly-allocated C-string, consisting of s, but with all final whitespace characters removed*
Code:
char* trimrear(char *s) (
{
char *s;
s=str;
while(*s){ (
if(isspace(*s))
s++;
else
break;
)
return s;
}
4. char* vowels(char *s);
*Here, s is a C-string. Its return value should be a newly-allocated C-string consisting of all the vowels found in s, in the order found, taking case into account.*
(somebody was helping with this for me, but there seems to be a problem with the strcat)
Code:
{
char* out = "";
for (i=0;i<strlen(s);i++)
{
char a = s[i];
switch (a)
{
case 'A':
case 'a':
case 'E':
case 'e':
// similarly i,o,u
strcat(out,a);
default: ;
} // end switch
} // end for
return out;
}
Note: I am aware that there will be segmentation faults unless I include the new operator.
Thanks to everybody who helps..