Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Basic help with C


stealth-'s Avatar
Ninja Extreme
0 0

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:

#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.

ghost's Avatar
0 0

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.com/6Zc7k0CM If you have any questions about what's going on in the code feel free to ask :D .


stealth-'s Avatar
Ninja Extreme
0 0

Awesome, thanks :)

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.


ghost's Avatar
0 0

stealth- wrote: Awesome, thanks :)

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.cplusplus.com/reference/


stealth-'s Avatar
Ninja Extreme
0 0

No, I havn't heard of that site. When I say new to C, I mean, like 20 pages into the book :P 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 :D


ghost's Avatar
0 0

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.


ghost's Avatar
0 0

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)

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


ghost's Avatar
0 0

++x = Prefix increment operator. x++ = Postfix increment operator. Read more about pre/post-fix operators here: http://www.acm.uiuc.edu/webmonkeys/book/c_guide/1.5.html

Here is my code suggestion:

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

#define MAX_LENGTH 100

void reverse(const char *pBuffer);

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

  if( (fp = fopen(&quot;words.txt&quot;, &quot;r&quot;)) == 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 &#92;n */

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