Why it doesn't show me strings like " a"?
Why is the while loop in the trim function bugged (if I input 2 chars the next input length can't be less than 2)?
#include <stdio.h>
#define MAX 1001
int getLine(char s[], int length){ // returns 1 or 0
int i, qttWord = 0; //qttWord = counter of letters for s[]
int c; // c = getchar()
/*Reads the input and puts it into s[], then, verifies if the input is just \n,
* if so, returns 0(i), if not, puts '\0' at the end of the string.
*/
for (i = 0; i < length-1 && (c = getchar()) != EOF && c != '\n'; ++i){
s[i] = c;
++qttWord;
}
if (i == 0){
return 0;
} else if (c == '\n'){
++i;
s[i] = '\0';
}
/*Verifies if the string is just ' ' or '\t'
* if so, returns 0
*/
char flag = '\0';
for (i = 0; i < qttWord && flag != '1'; ++i){
if (s[i] == ' ' || s[i] == '\t'){
flag = '0';
} else{
flag = '1';
}
}
if (flag == '0')
return 0;
return qttWord;
}
void trim(char s[], int length){
char s2[MAX];
int i, qttWord = 0;
/*while (s[length] != '\0'){
++length;
}
printf("length:%d\n", length);*/
for (i = 0; i < length; ++i){
if (i < length-1){
if (s[i] == ' ' && s[i+1] != ' '){
s2[i] = s[i];
++qttWord;
printf("1:%d\n", s2[i]);// if true prints "1" and the character(' ')
}
}
if (s[i] != ' '){
s2[i] = s[i];
++qttWord;
printf("0:%d\n", s2[i]);//if true prints "0" and the character
}
}
s[0] = '\0';
s2[qttWord] = '\0';
for (i = 0; i < qttWord; ++i){
s[i] = s2[i];
}
s2[0] = '\0';
}
>>38
A char is a 1-byte variable. As long as it's 1 byte wide, you can put anything on it.
This includes 'a' (01100001 in ASCII), 1 (00000001), '1' (00110001) or 255 (11111111).
Name:
Anonymous2012-11-03 22:46
>>41
In C you only have 7 guaranteed bits in a char, you must use signed or unsigned to get the full 8 bits, char is the only integer type that behaves like this.
It's an integer type just like short, long, int and whatnot. It is ``meant for'' holding characters, but it can hold any integer value within its range.
char is not necessarily 8 bits. int is not necessarily larger than char. sizeof(char) is guaranteed to be 1, however, meaning the sizes of all other types must be multiples of the size of char.
>>43 char is not necessarily 8 bits.
whatafuck man
No, seriously, if sizeof(char) is guaranteed to be 1, why wouldn't char be 8 bits?
Name:
Anonymous2012-11-03 23:02
On some machines the ``byte'', or smallest easily-addressable unit of memory, is not 8 bits or even a power of two. I believe the PDP-11 had 9-bit bytes, and the C compiler for some old Cray used 32- or even 64-bit chars because anything smaller would have required a whole bunch of mask and shift operations for each char access, making it unbelievably slow.
Old IBM machines had 6-bit bytes and 36-bit words, but I'm not sure if C guarantees a minimum width of char that would require it to use two bits on those machines.
>>45
sizeof(char)*CHAR_BITS is the only reliable way of knowing
Name:
Anonymous2012-11-03 23:13
Ok, found the solution.
Although, I don't think what I did with thes2 string is very virtuous...
#include <stdio.h>
#define MAX 1001
int getLine(char s[], int length){ // returns 1 or 0
int i, qttWord = 0; //qttWord = counter of letters for s[]
int c; // c = getchar()
/*Reads the input and puts it into s[], then, verifies if the input is just \n,
* if so, returns 0(i), if not, puts '\0' at the end of the string.
*/
for (i = 0; i < length-1 && (c = getchar()) != EOF && c != '\n'; ++i){
s[i] = c;
++qttWord;
}
if (i == 0)
return 0;
else
s[i] = '\0';
/*Verifies if the string is just ' ' or '\t'
* if so, returns 0
*/
for (i = 0; i < qttWord; ++i){
if (s[i] != ' ' || s[i] != '\t')
return 1;
}
return 0;
}
void trim(char s[]){
int i, qttWord = 0, length = 0;
while (s[length] != '\0'){
++length;
}
char s2[length];
for (i = 0; i < length; ++i)
s2[i] = -1;
for (i = 0; i < length; ++i){
if (i < length-1){
if (s[i] == ' ' && s[i+1] != ' '){
int ii;
for (ii = 0; ii < length; ++ii){
if (s2[ii] == -1){
s2[ii] = s[i];
++qttWord;
printf("1:%c\n", s2[ii]);
break;
}
}
}
}
if (s[i] != ' '){
int ii;
for (ii = 0; ii < length; ++ii){
if (s2[ii] == -1){
s2[ii] = s[i];
++qttWord;
printf("0:%c\n", s2[ii]);
break;
}
}
}
}
s[0] = '\0';
s2[qttWord] = '\0';
printf("string2:%s\n", s2);
for (i = 0; i < qttWord; ++i){
s[i] = s2[i];
printf("s:%c\n", s[i]);
}
s[qttWord] = '\0';
printf("string:%s\n", s);
s2[0] = '\0';
}
in getline:
/*Verifies if the string is just ' ' or '\t'
* if so, returns 0
*/
for (i = 0; i < qttWord; ++i){
if (s[i] != ' ' || s[i] != '\t')
return 1;
}
return 0;
This is backwards -- it returns 1 for the string " a".
in trim
for (i = 0; i < length; ++i){
if (i < length-1){
This is redundant.
Name:
Anonymous2012-11-03 23:39
>>51 it returns 1 for the string " a".
It should. in trim
for (i = 0; i < length; ++i){
if (i < length-1){ This is redundant.
Nope.
Name:
Anonymous2012-11-04 0:02
>>43 It's an integer type just like short, long, int and whatnot. It is ``meant for'' holding characters, but it can hold any integer value within its range.
I never said it wasn't an integer type, however it's not a "signed integer type" which the other types you listed are, signed char is a signed integer type.
If you write short int, int or long int you know that they are signed, you don't know that about char, whether char is signed or unsigned is up to the implementation so if you want to be portable you can only work with the 0-127 range i.e. 7 bits unless you specify with either the signed or unsigned type specifiers.
char is not necessarily 8 bits.
Right, it has to be able to hold at least 8 bits.
int is not necessarily larger than char.
Right, but why are you telling me this?
sizeof(char) is guaranteed to be 1, however, meaning the sizes of all other types must be multiples of the size of char.
Right, but why are you telling me this?
>>45 No, seriously, if sizeof(char) is guaranteed to be 1, why wouldn't char be 8 bits?
It's at least 8 bits, of which you can only 7 bits if you want to be portable when you're using the unspecified char type.
On systems that support the optional types in stdint.h like int8_t and uint8_t CHAR_BIT is exactly 8.
>>53 Right, it has to be able to hold at least 8 bits.
Nitpick, but only since C99. Earlier standards just require that it can hold at least 95 values, because it has to be able to hold the basic execution character set. In principle, 7-bit (or 5-trit) chars are possible.
Name:
Anonymous2012-11-04 0:42
>>54
In my version of the ANSI C standard CHAR_BIT is guaranteed to be at least 8.
for (i = 0; i < length-1 && (c = getchar()) != EOF && c != '\n'; ++i){
s[i] = c;
++qttWord;
}
The above is bullshit, code like a !nigger
for (i = 0; i < length-1;)
{
c = getchar())
if(c == EOF || c == '\n')
break;
s[i++] = c;
s[i] = '\0';
}
Name:
Anonymous2012-11-04 6:08
Symta:
yoba M = O:<F [V X@T]={F V X=[V@(r F [X@T])];[V]};F X=X>
= hyp 0 0 M,0,len M,len
| m:[X Y] (drop Y M |m (drop X ? | O `≤≤`) | O <A B = B-A | all ?≥≥0>)
| sort by:?,len*?,0,len | rhd
C/C++:
#include <stdio.h>
typedef int bool;
#define true 1
#define false 0
struct solution {
int top_left_x;
int top_left_y;
int width;
int height;
};
int main(int argc,char** argv)
{
const int x_size = 4;
const int y_size = 4;
int data[4][4] = {{9,4,5,5},{5,1,3,3},{8,1,4,5},{8,0,5,2}};
int top_left_x;
int top_left_y;
int width;
int height;
for (width=1;width<x_size;width++)
{
for (height=1;height<y_size;height++)
{
for (top_left_x=0;top_left_x<x_size-width;top_left_x++)
{
for (top_left_y=0;top_left_y<y_size-height;top_left_y++)
{
bool passed = true;
int i,j;
//x check
for (i=0;(i<width) && passed;i++)
{
for (j=0;(j<height+1) && passed;j++)
{
int k = i + top_left_x;
int l = j + top_left_y;
int current = data[l][k];
int next = data[l][k+1];
if (current>next)
passed = false;
}
}
//y check
for (i=0;(i<width+1) && passed;i++)
{
for (j=0;(j<height) && passed;j++)
{
int k = i + top_left_x;
int l = j + top_left_y;
int current = data[l][k];
int next = data[l+1][k];
if (current>next)
passed = false;
}
}
int i,j;
if (sol.top_left_x>-1)
{
for (j=0;j<sol.height+1;j++)
{
for (i=0;i<sol.width+1;i++)
{
printf("%d ",data[j+sol.top_left_y][i+sol.top_left_x]);
}
printf("\n");
}
}
return 0;
}
Name:
Anonymous2012-11-04 6:51
>>60
You don't fuck with my rights to privacy, cretin.
Name:
Anonymous2012-11-04 8:30
>>61
When antisemitism becomes global, crowds will be lynching, you, Jews all around the globe right inside your rich Jewish houses. Anyway, having a private house is immodest and wasteful. Everyone should live in commies blocks.
I don't know what point you're making here. The second code snippet is readable, the first one isn't. The programs also aren't equivalent. Symta looks... interesting, though.