String truncation functions and applications in C Language

Source: Internet
Author: User


/* ===================================================== ============================
Child integer
Source program name num .??? (PAS, C, CPP)
Executable File Name num.exe
Input File Name num. In
Output file name num. Out
For a five-digit a1a2a3a4a5, you can split it into three child numbers:
Sub1 = a1a2a3
Sub2 = a2a3a4
Sub3 = a3a4a5
For example, a five-digit 20207 can be split
Sub1= 202
Sub2 = 020 (= 20)
Sub3 = 207
Given a positive integer k, you must program to find all the five digits that meet the following conditions between 10000 and 30000,
The condition is that the three sub-numbers sub1, sub2, and sub3 of these five-digit numbers can be divisible by K.
Input
Input is input by the keyboard. Only one line is entered, which is a positive integer K (0 <k <1000 ).
Output
Output to the file. Each line of the output file is a five-digit meeting the conditions, which must be output from small to large.
Repeated output or omission is not allowed. If no solution is available, "no" is output ".

Example
Num. In
15
Num. Out
22555
25555
28555
30000

========================================================== ============================= */

# Include <stdio. h>
# Include <string. h>

/* Extract n characters from the left of the string */
Char * Left (char * DST, char * SRC, int N)
{
Char * P = SRC;
Char * q = DST;
Int Len = strlen (SRC );
If (n> Len) n = Len;
/* P + = (LEN-N); * // * starts from the nth character on the right */
While (n --) * (Q ++) = * (p ++ );
* (Q ++) = '\ 0';/* is it necessary? It is necessary */
Return DST;
}

/* Extract n characters from the middle of the string */
Char * mid (char * DST, char * SRC, int N, int M)/* n is the length, and m is the position */
{
Char * P = SRC;
Char * q = DST;
Int Len = strlen (SRC );
If (n> Len) n = len-m;/* from the MTH to the last */
If (M <0) m = 0;/* starts from the first */
If (M> Len) return NULL;
P + = m;
While (n --) * (Q ++) = * (p ++ );
* (Q ++) = '\ 0';/* is it necessary? It is necessary */
Return DST;
}

/* Extract n characters from the right of the string */
Char * Right (char * DST, char * SRC, int N)
{
Char * P = SRC;
Char * q = DST;
Int Len = strlen (SRC );
If (n> Len) n = Len;
P + = (LEN-N);/* starts from the nth character on the right */
While (* (Q ++) = * (p ++ ));
Return DST;
}

Void main ()
{
File * P;
Int I, K, Outi, Count = 0;
Int sub1, sub2, sub3;
Char * strsub1, * strsub2, * strsub3, * strtempnum, * a, * B, * C;

If (P = fopen ("num. Out", "AB +") = NULL)
{
Printf ("Open File fail! ");
Getch ();
Exit ();
}
Printf ("Please input int number (0 <k <1000 ):");
Scanf ("% d", & K );

For (Outi = 10000; Outi <= 30000; Outi ++)
{
ITOA (Outi, strtempnum, 10 );

Left (strsub1, strtempnum, 3 );
Mid (strsub2, strtempnum, 3, 1 );
Right (strsub3, strtempnum, 3 );

/*
A = strsub1;
B = strsub2;
C = strsub3;
Printf ("strsub1 = % s, strsub2 = % s, strsub3 = % s \ n", A, B, C );
*/

Sub1 = atoi (strsub1 );
Sub2 = atoi (strsub2 );
Sub3 = atoi (strsub3 );

/*
Printf ("sub1 = % d, sub2 = % d, sub3 = % d \ n", sub1, sub2, sub3 );
Printf ("sub1k = % d, sub2k = % d, sub3k = % d \ n", sub1 % K, sub2 % K, sub3 % K );
Getch ();
*/
If (sub1% K) = 0 & (sub2% K) = 0 & (sub3% K) = 0)
{
Fprintf (P, "% d \ n", Outi );
Count ++;
Printf ("Outi = % d \ n", Outi );
}
Else
{
Fprintf (P, "% s \ n", "no ");
}
}
Printf ("Count = % d OK", count );
Fclose (P );
Getch ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.