#include
#include
# define MAX 100
Char s (MAX), t (MAX);
Int length (char [s]) {
int i;
for (i=0; S [I]!='\ 0'; I++);
Return (I);
}
Int sub (char [] s, char t [], int start, int len) {
Int n, I;
N=length (s);
If (start & lt; 0 | | start>=n)
Return (0);
If (len<0 | | len + start> N)
Return (0);
for (i=0; i
T [I]='\ 0';
Printf (" intercept the string: ");
for (i=0; T [I]!='\ 0'; I++);
Printf (" % c ", [I] t);
The return (1);
}
Int main () {
int i;
Printf (" please input a character: ");
for (i=0; S [I]!='\ 0'; I++);
The scanf (" % c ", & amp; S [I]);
Sub (s, t, 3, 5);
return 0;
}
CodePudding user response:
In fact, your code in the input processing problems,for (i=0; S [I]!='\ 0'; I++);
It has several problems,
1. S if you define the array is a global variable, the compiler will generally give initialized to zero, namely each element of the array is 0, if is defined by a local variable is not initialized, which is assigned to the dirty data in the stack space, on the concept of stack and heap, the concept of global and local, if don't understand, you can find books to read, these are the basic,
That is to say, you write the code, I=0, s [I] is 0 (the character '\ 0' is the meaning of 0)
2, a semicolon (;) Can't disorderly use, semicolons means the end of the said statement,
Behind the for statement directly write a semicolon, you said for statement execution loop condition judgment and i++, until the end of the conditions, not do other things,
At the end of this for I=0, (your case)
3. After the above for thoroughly, to perform the scanf (" % c ", & amp; S [I]); And it is with you for statement is the same level, because you have the for end with a semicolon,
This after an execution, first I=0 s array element is set up one character at a time, s array is a string of length 1,
So, you want to begin from the index=3, obtains the substring of length=5, according to your code processing, natural have no results,
I want to, do you want to:
1. Enter a string, this should be the scanf (" % s ", s); % s is said input string
If use % c one character one character at a time, you need to end conditions, such as code to become like the following:
int i;
char c;
I=0;
The scanf (" % c ", & amp; C);
While (c!='e' & amp; & i
The scanf (" % c ", & amp; C);
}
S [I]=0;/* the end of the string plus 0 */
2. The rest is to call your code to handle strings,
As a student, how do exercises can be written, in the actual business development, is your writing the code can not completely,
So, the student what to learn?
In fact, is to "principle", to know every language of its internal processing mechanism, which is the most basic functions, the compiler
To understand the basic principle, to master this language writing features, in the end, combining with the experience constantly, will be able to write good code,
Now, language is various, each has its advantages and disadvantages, to learn to draw the essence, is the "principle", then, can quickly write code,
CodePudding user response:
Comment on penetrating upstairsCodePudding user response: