Follow us on Twitter!
The measure of a mans life is not how well he dies, but how well he lives.
Wednesday, April 16, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 22
Guests Online: 18
Members Online: 4

Registered Members: 82811
Newest Member: IsaiahBowman
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Basic help with C

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 12-03-10 08:05
Hey guys,

Here I am, attempting to learn C. So far, it's not working out to great :(
I was given the "challenge" of creating a function that can reverse a line of text and then implementing it in a program that will go through a file one line at a time and print out the reverse of each line. I have it working, to a degree.

Here is the code I am currently using:
Code

#include <stdio.h>
#define MAX 800
int getlines(char chars[]);
void reverse(char chars[],int length);
int main()
{
   char stri[MAX];
   int strilength;
   strilength = getlines(stri);
   reverse(stri,strilength);
   int i;
   printf("%s\n",stri);
   return 0;
}
int getlines(char stri[])
{
   int c, i;
   for (i = 0; (c = getchar()) != EOF && c != '\n'; ++i)
      stri[i] = c;
   --i;
   return i;
}
void reverse(char chars[], int length)
{
   char tempstri[MAX];
   int downlength;
   downlength = length;
   int i;
   for (i = 0; i != length; ++i)
   {
      tempstri[i] = chars[downlength];
      --downlength;
   }
   for (i = 0; i != length; ++i)
      chars[i] =  tempstri[i];
}





The code is currently only designed to process the first line of a file, and you input the file via stdin. So: cat example.txt | ./reverse

I had it working great, the phrase "testing" correctly reversed itself. So I tried a bigger phrase ("testing123"), and the application starts spewing out "gesting" backwards, and then a bunch of gibberish. I thought this could maybe be caused by having #define MAX 800 too small, so I changed it to some ridiculously big number just to be sure, and the code kind of worked. Then, it started giving me just "gesting" backwards, and this time no extra random output. So I changed the code back to #define MAX 800, and now it only gives me "gesting" backwards, still! I didn't change anything else, so you can see how really confused I am.

I think what is happening is the last character of the phrase is overwriting the first, somehow. Because "testinz" resulted in "zestinz", backwards aswell.

If anyone could give any help it would be very much appreciated.

Before anyone responds though, I understand my code is probably written like complete crap, and while I am looking to improve, please keep it productive.


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealt. . .

Edited by stealth- on 12-03-10 08:11
http://www.stealth-x.com
Author

RE: Basic help with C


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 12-03-10 16:53
This is how I would go about solving the given challenge. (I did this rather quick, I have to go to class soon but..) http://pastebin.c. . . If you have any questions about what's going on in the code feel free to ask Grin .



Author

RE: Basic help with C

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 12-03-10 18:37
Awesome, thanks Smile

Although I havn't gotten to the part that covers files and memset yet in the tutorial yet, I can figure out what it does. One question I do have though, is x++ and length-- the same as ++x and --length? They have different meanings in python, I'm not sure if it's the same for C.


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealt. . .

Edited by stealth- on 12-03-10 18:37
http://www.stealth-x.com
Author

RE: Basic help with C


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 12-03-10 19:13
stealth- wrote:
Awesome, thanks Smile

Although I havn't gotten to the part that covers files and memset yet in the tutorial yet, I can figure out what it does. One question I do have though, is x++ and length-- the same as ++x and --length? They have different meanings in python, I'm not sure if it's the same for C.


Yes ++x and x++ are different. In a loop x++ adds one to x after it goes through the code once. ++x adds one to x then goes through the code. O and you probably know this site but I use it all the time. It's really helpful http://www.cplusp. . .reference/


Author

RE: Basic help with C

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 12-03-10 20:25
No, I havn't heard of that site. When I say new to C, I mean, like 20 pages into the book Pfft Anywho, I've got to leave town in the next couple hours so I'm compare our code over the weekend and I'm sure I'll figure it out.

Thanks again Grin


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealt. . .

Edited by stealth- on 12-03-10 20:26
http://www.stealth-x.com
Author

RE: Basic help with C


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 13-03-10 07:31
skathgh420 wrote:
Yes ++x and x++ are different. In a loop x++ adds one to x after it goes through the code once. ++x adds one to x then goes through the code.

I'd like to add to what C master skath probably just forgot to mention here and say that that is valid for not only loops, but for every line/expression/whatever in your code.


Author

RE: Basic help with C


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 16-03-10 12:57
i can recommend that you use stack's i this exampel as they ease things up alot when reversing a string, an exampel could look something like this(quick and messy version)

Code

void reverse(char RVS[]);
int main()
{
   cout << "write string to reverse: ";
   char rvs[356];
   cin >> rvs;
   reverse(rvs);
   return 0;
}

void reverse(char RVS[])
{
   char c;
   stack<char> temp;
   for(int n = 0; n < strlen(RVS); n++){
      temp.push(RVS[n]);
   }
   for(int i = 0; i < strlen(RVS); i++){
      cout << temp.top();
      temp.pop();
   }
   cout << endl;
}





ooh and will need to #include <stack>... i think, i am on windows at the moment and has not done so much programming in linux

Edited by on 17-03-10 11:15
Author

RE: Basic help with C

doowzer
Member

Your avatar

Posts: 2
Location: AF_LOCAL
Joined: 27.11.08
Rank:
Moderate
Posted on 17-03-10 23:06
++x = Prefix increment operator.
x++ = Postfix increment operator.
Read more about pre/post-fix operators here: http://www.acm.ui. . .e/1.5.html

Here is my code suggestion:

Code

#include <stdio.h>
#include <string.h>

#define MAX_LENGTH 100

void reverse(const char *pBuffer);

int main(int argc, char *argv[]) {
  FILE *fp;
  char rgBuffer[MAX_LENGTH];

  if( (fp = fopen("words.txt", "r")) == NULL )
    return (-1);
 
  while( fgets(rgBuffer, MAX_LENGTH, fp) != NULL ) {
    reverse(rgBuffer);
    putchar(0xa);
  }

  fclose(fp);
  return (0);
}

void reverse(const char *pBuffer) {
  int nIndex = (strlen(pBuffer) - 1); /* skip \n */

  while( nIndex-- > 0 )
    putchar(*(pBuffer + nIndex));
}










"Live as if your were to die tomorrow. Learn as if you were to live forever."

Edited by doowzer on 17-03-10 23:11