Memory usage v/s complexity -- which one better?

  • Thread starter Himanshu Singh Chauhan
  • Start date
H

Himanshu Singh Chauhan

Hello all!

I am a newbie in C world.
I was making a program from K&R (1-17).
I made 2 functions and found that
same problem could be solved with
1. Making new varibles (array, 2 or more).
2. Using same available array but
by increasing the complexity.

I was thinking which would be better option?
I always try to keep number of variables as low
as possible, though I know Memory is not a problem
these days.
I am right in doing this.

Your help required.

TIA

Regards
--Himanshu
 
J

j

Himanshu Singh Chauhan said:
Hello all!

I am a newbie in C world.
I was making a program from K&R (1-17).
I made 2 functions and found that
same problem could be solved with
1. Making new varibles (array, 2 or more).
2. Using same available array but
by increasing the complexity.

I was thinking which would be better option?
I always try to keep number of variables as low
as possible, though I know Memory is not a problem
these days.
I am right in doing this.

Your help required.

TIA

You do not need extensive memory usage
or create a complex program to solve this.

Nor is there a need for more than one array.
You can simply use one array.

Below is my solution to exercise 1-17:

#include <stdio.h>

#define MAX_CHARS 80

int main(void)
{
int c, i;
char line[MAX_CHARS + 1];

for(i = 0; (c = getchar()) != EOF && c != '\n'; ++i) {
if(i >= MAX_CHARS) {
if(i == MAX_CHARS) {
line = '\0';
printf("%s", line);
}
putchar(c);
} else
line = c;
}

return 0;
}
 
E

Emmanuel Delahaye

Below is my solution to exercise 1-17:

<snipped>

I don't think it's a good idea to post a complete solution to a newbie that
have not posted even a line of C...

Better to teach fishing that to offer a fish. Don't you think so?
 
H

Himanshu Singh Chauhan

Sorry! this might be frustrating but question was 1-18 and I am trying
it another way. Question is for trailing blank spaces of lines. I am
trying it everywhere in the line.
I am posting my program. I would like to know where my programming
practices are wrong.

TIA.

#include <stdio.h>

#define MAXLEN 1000

int getline(char s[], int len);
int format(char s[], int len);

int
main()
{
int len;
char line[MAXLEN];
while ((len = getline(line, MAXLEN)) > 0) {
format(line, len);
}

return (0);
}

int
getline(char s[], int len)
{
int c, i = 0;

while ((c = getchar()) != EOF && i < len - 1) {
s = c;
++i;
}
if (i == len - 1)
s = '\0';
else if (c == EOF) {
s = '\0';
}

return (i);
}

int
format(char s[], int len)
{
int i = 0;
char tmp[len];
int j = 0;

for (j = 0; j < len; ++j)
tmp[j] = ' ';

j = 0;

while (s != '\0') {

if ((s[i + 1] == ' ' || s[i + 1] == '\t') && s == '\t') {
if (s[i + 1] == '\t' || s[i + 1] == ' ') {
s = ' ';
++i;
} else
++i;
} else if ((s[i + 1] == ' ' || s[i + 1] == '\t') && s == ' ')
++i;
else {
tmp[j] = s;
++i;
++j;
}

}

tmp[j] = '\0';
printf("%s", tmp);

return (0);
}

Please tell if I am practising wrong somewhere.

THanx

regards
--Himanshu
 
B

Barry Schwarz

Sorry! this might be frustrating but question was 1-18 and I am trying
it another way. Question is for trailing blank spaces of lines. I am
trying it everywhere in the line.
I am posting my program. I would like to know where my programming
practices are wrong.

TIA.

#include <stdio.h>

#define MAXLEN 1000

int getline(char s[], int len);
int format(char s[], int len);

int
main()
{
int len;
char line[MAXLEN];
while ((len = getline(line, MAXLEN)) > 0) {
format(line, len);
}

return (0);
}

int
getline(char s[], int len)

I guess K&R don't mention it this early but there is a standard C
function called fgets that does what you want.
{
int c, i = 0;

while ((c = getchar()) != EOF && i < len - 1) {

On most home systems, getchar reads from the keyboard. Keyboard input
is usually buffered until the user presses the ENTER key or
equivalent. This normally does not generate an EOF condition. The
expected event is a '\n' character.
s = c;
++i;
}
if (i == len - 1)
s = '\0';
else if (c == EOF) {
s = '\0';


There are two ways the previous while loop could terminate. Your if
and else test for both conditions. But you take the same action
regardless of which condition is true. Since you don't really care
about the condition, only that the loop has terminated, you can omit
the tests and simply take the action.
}

return (i);
}

int
format(char s[], int len)
{
int i = 0;

Exercise 1-18 deals with removing trailing blanks and tabs. By
starting with 0, you are dealing with leading characters.
char tmp[len];
int j = 0;

for (j = 0; j < len; ++j)
tmp[j] = ' ';

j = 0;

while (s != '\0') {

if ((s[i + 1] == ' ' || s[i + 1] == '\t') && s == '\t') {
if (s[i + 1] == '\t' || s[i + 1] == ' ') {
s = ' ';
++i;
} else
++i;
} else if ((s[i + 1] == ' ' || s[i + 1] == '\t') && s == ' ')
++i;
else {
tmp[j] = s;
++i;
++j;
}


Your attacking the wrong problem and consequently working much too
hard. Your code seems to be dealing with consecutive blanks and tabs
but does not distinguish between trailing ones and imbedded/leading
ones.
}

tmp[j] = '\0';

Possibly undefined behavior depending on the user input. Consider if
the user enters abc with no blanks:

getline stored the characters 'a', 'b', 'c', and '\0' is s[0]
through s[3] which happens to be line[0] through line[3] in main and
returns the value 3.

In format, you define tmp to be an array of 3 char and store the
'a', 'b', and 'c' int tmp[0] through tmp[2]. In this statement, you
are attempting to store a '\0' in tmp[3] which does not exist.
printf("%s", tmp);

You print the line even if it was all blank. The exercise
specifically requested completely blank lines be deleted (not
printed).
return (0);
}

Since a string is simply a sequence of characters terminated with a
'\0', truncating characters from the end of the string is nothing more
than placing a '\0' somewhere sooner in the sequence. Consider

void format(char s[], int len){
int i = len-1;
while (i >= 0 && (s == ' ' || s == '\t'))
i--;
s[i+1] = '/0'
}

Since this function updates line in main directly, I would move the
printf up to main after calling format. (This makes the function a
more general "trailing blank remover" and lets the calling function
decide what to do with the truncated string.) You would also need to
add a check to skip the printf if line[0] == '\0'.

Note that the initialization of i and the two statement while loop
could be replaced by

for (i = len-1; i >= 0 && (s == ' ' || s == '\t'); i--)
;


<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top