new here

TxCobrA98

Beta member
Messages
5
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!
 
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
 
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
 
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..
 
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
 
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.
 
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
 
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..
 
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!
 
Back
Top Bottom