![]() |
I'm working on a c++ assignment and i need a little help. I have to compare two end slots in a char array and work my way in ex. first with last, second with second to last ect., then return wheather all the charecter match or not. I can't figure out how to exactly to do it. I know it has something to do with a loop and an if statement but how how exactly i can't figure out.
|
Sorry, I have no clue, and I spam this just to promote Java. It makes everything so much easier.
|
don't know java, plus this is a c++ class so [img]tongue.gif[/img] its fun and not to hard just time consuming. Less now though that i'm using Visual Basic :D great program and you can use lots of languages in it.
|
</font><blockquote>code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">for(n:=0,m:=len(str)-1;n>m;n++,m--)
{ // your camparison goes in here }</pre>[/QUOTE]Ye gods but I havent used this stuff in while. That for loop is fairly simple -initialise n and m to the beginning and end of your string, end the loop when n is more than m, each loop increment n and decrement m. But I think youll need to check the logic, run through it (on paper) with odd and even length strings. And get a textbook and make sure that thats the correct syntax for a two-variable for loop, I havent used one in *years*. Easiest way to return the result is </font><blockquote>code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">result:=true; for(n:=0,m:=len(str)-1;n>m;n++,m--) { if(str[n]!=str[m] then result:=false; } return(result);</pre>[/QUOTE]Heh - as you can see Im confusing Pascal and C++ syntax. Trouble with switching languages so often [img]smile.gif[/img] Anyway, that bit is fairly standard logic. Only one fail is needed to fail the whole lot, so you may want to optimise with a break statement, but I woudnt bother outside of a performance critical function, which this isnt as its a homework problem. |
Andrewas, you really ought to add something like "Official computer expert of IW" or something in your signature! [img]graemlins/thumbsup.gif[/img] BTW andrewas, do you know if I can manually add the acrobat script to Word XP? The one that allows you to convert to PDF without changing printer properties? (I think it´s called acrobat.dot) [img]smile.gif[/img]
|
Is this an assignment to find out if a word is a palindrome? if it is, do NOT compare 1st to last, 2nd to 2nd last, etc. instead, reverse your original string and then compare the two...
|
well i allready did that for on funcition, we need to do it two different ways. Sorry for the late reply i have been of doing things. I'll give it a try andrewas
|
well here i would post my code here so people could confuse themselves but it says html code error, doesn't allow parenthasis on my :( o well thank for the help, it works. I knew it would just coudn't figure out how to put it into code thanks.
|
well you do know that Java and C++ are cousins...
|
there are lots of simular languages there Death, c++, java, and basicaly any general overall languages are related. They all take the code and encode it into the same basic language, that is how all the .net stuff works you know.
|
All times are GMT -4. The time now is 08:43 AM. |
Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
©2024 Ironworks Gaming & ©2024 The Great Escape Studios TM - All Rights Reserved