Home > Back-end >  The length of the input string to ask, but I wrote it only reply of length 1, don't know why.
The length of the input string to ask, but I wrote it only reply of length 1, don't know why.

Time:10-03

# include & lt; Stdio. H>
# include & lt; stdlib.h>
Int size=100;
Int strlen (int *);
Int main ()
{
Int * a=NULL, I=0;
A=(int *) malloc (size * sizeof (int));
If (a==NULL)
return -1;
While (the scanf (" % d ", a + I)==1)
{
I + +;
If (I==size)
{
A=(int *) realloc (a, size);
Size=size * 2;
If (a==NULL)
return -1;
}
}
Int len=strlen (a);
Printf (" % d ", len);
If (a)
Free (a);
return 0;

}
Int strlen (int * p)
{
If (* p=='\ 0')
return 0;
The else
Return 1 + strlen (p + 1);
}

CodePudding user response:

 # include & lt; Stdio. H> 
# include & lt; stdlib.h>

Int size=100;
Int my_strlen (char *);

Int main ()
{
//int * a=NULL, I=0;
Int I=0;
Char * a=NULL;

A=(char *) malloc (size * sizeof (char));
If (a==NULL)
return -1;

While (the scanf (" % c ", a + I)==1)
{
I + +;
If (I==size)
{
A=(char *) realloc (a, size * 2 * sizeof (char));
Size=size * 2;
If (a==NULL)
return -1;
}
}
Printf (" % d \ n ", I);
A [I]=0;
Int len=my_strlen (a);
Printf (" % d ", len);
If (a)
Free (a);
return 0;

}
//int my_strlen (int * p)
Int my_strlen (char * p)
{
If (* p=='\ 0')
return 0;
The else
Return 1 + my_strlen (p + 1);
}

For your reference ~

The string is not the same as int, string is a char *

CodePudding user response:

 # include & lt; Stdio. H> 
# include & lt; stdlib.h>

Int size=100;
Int my_strlen (char *);

Int main ()
{
//int * a=NULL, I=0;
Int I=0;
Char * a=NULL;

A=(char *) malloc (size * sizeof (char));
If (a==NULL)
return -1;

While (the scanf (" % c ", a + I)==1 & amp; & A [I]! )
='\ n'{
I + +;
If (I==size)
{
A=(char *) realloc (a, size * 2 * sizeof (char));
Size=size * 2;
If (a==NULL)
return -1;
}
}
//printf (" % d \ n ", I);
Int len=my_strlen (a);
Printf (" % d ", len);
If (a)
Free (a);
return 0;

}
//int my_strlen (int * p)
Int my_strlen (char * p)
{
If (* p=='\ n')
return 0;
The else
Return 1 + my_strlen (p + 1);
}

Another version, for your reference ~

CodePudding user response:

Pay attention to the first version, need to exit the while loop, the method is Linux use CTRL + d, under Windows with CTRL + z
The second version, can be enter directly after the input string,

The end of the string is' \ 0 ', integer data with '\ 0' it doesn't matter,

CodePudding user response:

Thank you very much, but why can't use int to deposit characters, int open storage space is not than char big yao, this is really not understand

CodePudding user response:

While (the scanf (" % d ", a + I)==1)
Here specify the input integer, if the input is not a valid integer format, such as a letter, then the return value is zero, you can't perform while down
Even if you input the number, input the number in a row, it will be handled as an integer, the maximum length of the int type in 10
Not more than 10 Numbers, you will be considered only 1

The building code can be used to test the string, 123, 456, 789, 322
The number of integers in the test this string

If this is not the case, you should refer to the first floor of the code

In addition, about ralloc function,
A=(int *) realloc (a, size);
Size=size * 2;
These two lines written against the to reassign a double memory

CodePudding user response:

Not cannot use int to character, can save, take four bytes to save one of their own data, waste
To play, so you just transform the scanf the code
From % d to % c, under you the custom strlen is can
At this time, don't change the strlen parameters to char *, otherwise is
Because int is four bytes, char at its lowest level, only the first character is not zero, the second is 0, that is no matter how much you input is return 1

Why the original code has been return 1, estimate is your input number is not long enough, and more than 10, it is not 1
According to the above method, and the transformation of strlen, that's no matter what input will return 1

CodePudding user response:

Thank you very much, that is the realloc is directly in the first paragraph after add a memory, a total of two paragraphs, or directly in the original after a period of two pieces of memory, a total of three?
  • Related