Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 11-09-2007, 06:07 PM   #1
Beta Member
 
Join Date: Nov 2007
Posts: 5
Default new here

Hey guys, I am currently a student at University of North Texas and I am enrolled in CSCE 1020 which is the first course for computer science.

I have been doing pretty good and haven't needed much help but now I am reaching out to you guys in search of some help. I am not currently at my college right now as I have gone home for the weekend and I cannot figure something out.

What I am having problems with is writing my code. My objective in my lab is to find the last three letters of a "string".

So say the word is 'something'. I need to write a line of code that will return the last three letters. So "ing" would be returned to me.

What I have right now is this - im hoping im at least on the right track..

threelast = line.substr(line.length()-1);
cout << "Last three are: " << threelast << endl;

And here is my second last question of my Lab which is directly from my instructions.

Using a loop and the square-bracket [] notation to access each individual character in the string, display the string backwards (last character in the string first, next to last, etc). Remember that the character positions in a string are numbered starting with 0, so the first position in the string would be stringname[0], while the last position in the string would be stringname[stringname.length()-1]. Also remember that individual positions in a string can be referenced using integer variables, such as an integer counter for a loop that goes from stringname.length()-1 down to 0.

So when i input "SOMETHING" it needs to come back as "GNIHTEMOS"..

If you guys could please help me out I would be SOOOO thankful as this is due at midnight tonight. Thanks guys! and i look forward to contributing to the forum!
__________________

TxCobrA98 is offline   Reply With Quote
Old 11-09-2007, 07:28 PM   #2
In Runtime
 
CGfreak102's Avatar
 
Join Date: Apr 2007
Posts: 475
Default Re: new here

int length = 0;
string or apstring threelast = "";
string or apstring word = "";
cout<<"Enter in a word: ";
cin>>word;
length = word.length();
cout<<"The last three letters of the word are: "<<word.substr(length-3, length)<<endl;




well that should work, i remeber doing this in my high school class, but i dont have the whole program with me, its in the lab network, but as far as i can remember that should work, just try it and tell me if it doesnt or does
__________________

__________________
Core I7 920 OC'ed to 3.0Ghz, 6GB G.skill DDR3 1333, Intel DX58SO, 500GB seagate 7200RPM + 320GB seagate, EVGA 9800GTX+ , 600W PSU.
CGfreak102 is offline   Reply With Quote
Old 11-09-2007, 07:29 PM   #3
In Runtime
 
CGfreak102's Avatar
 
Join Date: Apr 2007
Posts: 475
Default Re: new here

why it is string or apstring, is because some people use different ones i think, or at least i was always told to use apstring
__________________
Core I7 920 OC'ed to 3.0Ghz, 6GB G.skill DDR3 1333, Intel DX58SO, 500GB seagate 7200RPM + 320GB seagate, EVGA 9800GTX+ , 600W PSU.
CGfreak102 is offline   Reply With Quote
Old 11-09-2007, 07:37 PM   #4
Beta Member
 
Join Date: Nov 2007
Posts: 5
Default Re: new here

hey man thanks for the response.. i actually figured that part out. now i just need the last part.

heres what i did for the first line.. i actually didn't know i could just plug in -3 for -1 lol...

threelast = line.substr(line.length()-3);
cout << "Last three are: " << threelast << endl;

where -3 is i kept using -1.... so it only returned G instead of now ING..
TxCobrA98 is offline   Reply With Quote
Old 11-09-2007, 07:47 PM   #5
In Runtime
 
CGfreak102's Avatar
 
Join Date: Apr 2007
Posts: 475
Default Re: new here

your way is a lot more compact than mine, lots better style, i like it,


anyways im working on the last part, we also went over it and its tricky and i wish i could just copy and paste it.

but if i figure it out, ill post it
__________________
Core I7 920 OC'ed to 3.0Ghz, 6GB G.skill DDR3 1333, Intel DX58SO, 500GB seagate 7200RPM + 320GB seagate, EVGA 9800GTX+ , 600W PSU.
CGfreak102 is offline   Reply With Quote
Old 11-09-2007, 08:05 PM   #6
Beta Member
 
Join Date: Nov 2007
Posts: 5
Default Re: new here

thanks man.. ive been working on the last few lines of code for about 4.5 hours now.. it took me about an hour to do the whole rest of the lab and im getting burnt out...

i think im getting close but then again, i have no idea lol. this is my first assignment with strings so im not the best at it.. but idk.

what i did for the loop problem is set

i need a counter to keep track of the number of characters in the string... so then ill do like a counter-- line until it reaches the 0 position in the string.. if you understand what im saying..

and im using while loop.. idk if thats right but im trying lol.
TxCobrA98 is offline   Reply With Quote
Old 11-09-2007, 08:08 PM   #7
In Runtime
 
CGfreak102's Avatar
 
Join Date: Apr 2007
Posts: 475
Default Re: new here

for the counter just use length = word.length();
that will find out how many characters are in the string, and then to kept track untill it reaches 0 use length--;, and every time it goes through the loop it takes one off the total, and then to see if it gets to 0 put in a if statement that looks like this

if(length == 0){
break;
(or exit, do what every you do to get out of the loop)
}



i think that is what your asking well at east what i think you are saying
__________________
Core I7 920 OC'ed to 3.0Ghz, 6GB G.skill DDR3 1333, Intel DX58SO, 500GB seagate 7200RPM + 320GB seagate, EVGA 9800GTX+ , 600W PSU.
CGfreak102 is offline   Reply With Quote
Old 11-09-2007, 08:44 PM   #8
Beta Member
 
Join Date: Nov 2007
Posts: 5
Default Re: new here

man i cant get this loop going..

OK, SOMETHING has 9 characters. I need to have some sort of counter on the number of characters so i can go from the last position (position 8) to the first character (position 0)..

i dont know how i can do that in a loop.. maybe its because ive been doing this stupid Lab for so long but my brain isn't giving me the answers..

so i need to take SOMETHING and turn it into GNIHTEMOS..

counter needs to go from 8 to 0.. and in the middle display the character for the current counter. so counter loop 4 should produce a E..
TxCobrA98 is offline   Reply With Quote
Old 11-09-2007, 08:58 PM   #9
Beta Member
 
Join Date: Nov 2007
Posts: 5
Default Re: new here

counter = line.length();
while (counter > 0) {
cout << line[counter];
if (counter == 0);
else counter--;}

OK THIS IS ALMOSTTTTTTTTTTTT WORKING!!

that code above gives this output.

�gnihtemo

i am missing the S and what the heck is that question mark thing?? PLEASE HELP.... ive been doing this program for 6.5 hours and haven't taken a break.. it is due at midnight tonight. please help!
TxCobrA98 is offline   Reply With Quote
Old 11-11-2007, 05:47 PM   #10
In Runtime
 
AllThingsSacred's Avatar
 
Join Date: Aug 2007
Posts: 302
Default Re: new here

Back on topic. This is a useful resource for this question:
http://www.codeproject.com/useritems...asp?print=true

That covers most of the C++ STL <string> library in further detail, and it appears you are trying to use this here.
__________________

__________________
Patrick Moore
Born London, Live London, Die London
As a Londoner, I exercise my God given Right to do as I please
AllThingsSacred is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 11:23 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0