Function that returns last three characters of a string

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 5129 Views - Last Post: 19 March 2010 - 12:02 AM Rate Topic: -----

#1 Sunleth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-March 10

Function that returns last three characters of a string

Posted 17 March 2010 - 02:34 AM

Hi, all. I'm trying to write a function that receives a C-style string and returns a string comprised of the last three characters in the string (e.g., if the input is "hello" it returns "llo"). So far I have this:

#include <iostream>
using namespace std;

char *lastThree(const char *input)
{
     char result[4];
     
     for (int i = 0; i < 4; i++)
     result[i] = input[strlen(input) - (3 - i)];
     
     return result;
}

int main()
{
    cout << lastThree("hello") << endl;
    
    system("PAUSE");
    return 0;
}


Problem is, it prints nothing. I have no clue what I'm doing wrong... (I don't completely understand pointers yet...) Any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Function that returns last three characters of a string

#2 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: Function that returns last three characters of a string

Posted 17 March 2010 - 02:45 AM

If you don't have any problem to use string then you can use it :
string lastThree(string input)
{
     return input.substr(input.size() - 3);
}

And for this you should use #include <string>.
Was This Post Helpful? 0
  • +
  • -

#3 Sunleth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-March 10

Re: Function that returns last three characters of a string

Posted 17 March 2010 - 02:57 AM

I wish I could use the string class, but unfortunately it has to be a C-style string. =(
Was This Post Helpful? 0
  • +
  • -

#4 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: Function that returns last three characters of a string

Posted 17 March 2010 - 03:12 AM

View PostSunleth, on 17 March 2010 - 01:57 AM, said:

I wish I could use the string class, but unfortunately it has to be a C-style string. =(

Okay. Here it is :
#include <iostream>
#include <cstring>

using namespace std;

char *lastThree(int s, const char *input)
{
	char *ret = new char[s];
	
	for (int i = 0; i <= s; i++)
	{
		ret[i] = input[strlen(input) - (s - i)];
	}
	
	return ret;
}


int main()
{
	cout << lastThree(3, "hello") << endl;
    
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#5 Sunleth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-March 10

Re: Function that returns last three characters of a string

Posted 17 March 2010 - 03:20 AM

It works! Thanks so much for the help! ^^
Was This Post Helpful? 0
  • +
  • -

#6 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Function that returns last three characters of a string

Posted 17 March 2010 - 03:25 AM

View PostTapas Bose, on 17 March 2010 - 09:12 AM, said:

View PostSunleth, on 17 March 2010 - 01:57 AM, said:

I wish I could use the string class, but unfortunately it has to be a C-style string. =(

Okay. Here it is :
#include <iostream>
#include <cstring>

using namespace std;

char *lastThree(int s, const char *input)
{
	char *ret = new char[s];
	
	for (int i = 0; i <= s; i++)
	{
		ret[i] = input[strlen(input) - (s - i)];
	}
	
	return ret;
}


int main()
{
	cout << lastThree(3, "hello") << endl;
    
	return 0;
}


Way way to compilcated and you have a memory leak. Why not try this:


char * endString(int s, const char *input) {
   int length = strlen(input);
   if ( s > length ) s = length;
   return input[length-s];
}

int main() {
  cout << endString(3, "hello") << endl;  
  return 0;
}


This post has been edited by Martyn.Rae: 17 March 2010 - 03:26 AM

Was This Post Helpful? 0
  • +
  • -

#7 Sunleth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-March 10

Re: Function that returns last three characters of a string

Posted 17 March 2010 - 03:42 AM

View PostMartyn.Rae, on 17 March 2010 - 02:25 AM, said:

Way way to compilcated and you have a memory leak. Why not try this:


char * endString(int s, const char *input) {
   int length = strlen(input);
   if ( s > length ) s = length;
   return input[length-s];
}

int main() {
  cout << endString(3, "hello") << endl;  
  return 0;
}



When I try to run this program, I get an error saying "In function `char* endString(int, const char*)' invalid conversion from `const char' to `char*' for
return input[length-s];


Also, what's a memory leak?
Was This Post Helpful? 0
  • +
  • -

#8 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: Function that returns last three characters of a string

Posted 17 March 2010 - 03:47 AM

View PostMartyn.Rae, on 17 March 2010 - 02:25 AM, said:

Way way to compilcated and you have a memory leak. Why not try this:


char * endString(int s, const char *input) {
   int length = strlen(input);
   if ( s > length ) s = length;
   return input[length-s];
}

int main() {
  cout << endString(3, "hello") << endl;  
  return 0;
}


Error-full code.
tapas@My-Child:~/Programming$ g++ test4.cpp -o test4.o
test4.cpp: In function ‘char* endString(int, const char*)’:
test4.cpp:9: error: invalid conversion from ‘const char’ to ‘char*’
Was This Post Helpful? 0
  • +
  • -

#9 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: Function that returns last three characters of a string

Posted 17 March 2010 - 03:56 AM

You should make sting null terminated. Here it is :
#include <iostream>
#include <cstring>

using namespace std;

char *lastThree(int s, const char *input)
{
	char *ret = new char[s+1];
	
	for (int i = 0; i <= s; i++)
	{
		ret[i] = input[strlen(input) - (s - i)];
	}
	
	ret[s] = '\0'; // making null terminated
	
	return ret;
}

You can also try this:
char *lastThree(int s, const char *input)
{
	char *ret = new char[s+1];
	
	int len = strlen(input);
	
	strncpy(ret, input + len - s, s);
	
	ret[s] = '\0';
	
	return ret;
}

This post has been edited by Tapas Bose: 17 March 2010 - 04:01 AM

Was This Post Helpful? 0
  • +
  • -

#10 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: Function that returns last three characters of a string

Posted 17 March 2010 - 04:08 AM

You should add some methods for input validation.
Was This Post Helpful? 0
  • +
  • -

#11 Sunleth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-March 10

Re: Function that returns last three characters of a string

Posted 17 March 2010 - 04:14 AM

Validate how? Shouldn't it work for all characters?
Was This Post Helpful? 0
  • +
  • -

#12 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Function that returns last three characters of a string

Posted 17 March 2010 - 04:16 AM

I didn't say I tested it! I always leave that up to you newbies. It was the concept I was giving you.

But to make it easy for you guys here is code that I have tested.

const char * endString(int s, const char *input) { 
   int length = strlen(input); 
   if ( s > length ) s = length; 
   return const_cast<const char *>(&input[length-s]); 
} 
 
int main() { 
  cout << endString(3, "hello") << endl;   
  return 0; 
}



@Tapas Bose, I downmarked you because whilst your code was correct seen from a syntactical point of view, there were FIVE logical errors in your code which I felt were so bad. You make comments on other peoples code and give them logically correct information. Let us say we gave your version of the code this to work with:

    char *lastThree(int s, const char *input) 
    { 
        char *ret = new char[s]; 
         
        for (int i = 0; i <= s; i++) 
        { 
                ret[i] = input[strlen(input) - (s - i)]; 
        } 
         
        return ret; 
    } 

    cout << endString(3, "h") << endl; 



So, we would have 3, and input="h"

Now we would create ret as an array of 3 characters using char *ret = new char[s];. So where is the space for the NULL terminating character which you must have on the end of the string? This should have been char *ret = new char[s+1];. First logical error.

We the enter the for loop, which runs for i=0, i=1, i=2, and i=3 oops, one to many times methinks. So that should have been for (int i = 0; i < s; i++), that would loop 3 times if s=3 which it does.
Second logical error.

Next error in your logic is assuming that the length of the string given is larger than the number of characters you want off it's tail. So, uing my input we get using the first loop interation input[strlen(input) - (s - i)] which is input[1 - (3 - 0)] = -2. ret[0] = input[-2]!!!. Next iteration where i=1 would give us input[1 - (3 - 1)] = -1. ret[1] = input[-1]!!!. Next and final iteration where i = 2 would give us input[1 - (3 - 2)] = 0. ret[2] = input[0] which is 'h'. Third logical error.

Next, you have not terminated the character string you are returning. Fourth logical error.

Finally, the new operator has been used to return the string to the main function. But the main function does not delete it. Memory leak and the fifth logical error.

My advice to you Tapas Bose, is do not offer advice to anybody until you have learnt how to think logically. At least my solution whilst syntactically incorrect, was logically sound.
Was This Post Helpful? 1
  • +
  • -

#13 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: Function that returns last three characters of a string

Posted 17 March 2010 - 04:28 AM

@ Martyn.Rae, thank you for your precious suggestion, I will keep it in mind. If I write the code for myself then I should add some function(s) for input validation and after validating input main() function will call lastthree() function. Then there will be no such errors as you described. That is why I told OP that to add some input validation in his code.
Was This Post Helpful? 1
  • +
  • -

#14 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Function that returns last three characters of a string

Posted 17 March 2010 - 04:30 AM

I saw that after I had posted.
Was This Post Helpful? 0
  • +
  • -

#15 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: Function that returns last three characters of a string

Posted 17 March 2010 - 04:38 AM

View PostSunleth, on 17 March 2010 - 03:14 AM, said:

Validate how? Shouldn't it work for all characters?

I want to mean by validation that, suppose you are asking to user for entering a name. Then it is well to check whether the name only consists character or not. So here comes a sting validation. Again you instruct him/her to give an integer, then you should check it is an integer or not so add a module for integer validation. Then check whether the given integer is less than the size of the name or not. If each and every validation is okay then call your function, otherwise abort the program and display appropriate message.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2