Exercise 3-3

Source: Internet
Author: User

# Include <stdio. h>
# Include <stdlib. h>

/*
Compile the function char * expand (const char * ss, char * DS) to write stenographer symbols similar to a-Z in the string SS in the string DS
Expands to an equivalent complete list abcdef... Z. This function can process uppercase and lowercase letters and numbers, and can process
A-B-c, a-z0-9 And-a-Z and other similar situations. As the leading and trailing-character is outprinted.
Several examples are provided:
A-F> abcdef
F-A> fedcba
A-F-A> abcdefedcba
A-A> A-
A-9-0> a-9876543210
A -- B> A -- B
-A-c>-ABC
A-A> A-
> The character on the right is the converted character.
*/
Char * expand (const char * ss, char * DS)
{
Char * ds_head = Ds;
Const char * ITER, * iter_l;
Char temp;

If (Ss = NULL) return NULL;
If (DS = NULL) return NULL;/***** Exception !!! *******/
If (Ss = DS) return NULL;/* error, SS cannot be modified through ds to protect the original SS */
/*************************************** *******************/
If (* Ss = '-') * DS ++ = * ss ++;/* when the first position of the string is */
For (; * ss! = '/0'; SS ++)
{
If (* Ss = '-'){
Iter = ss-1; iter_l = SS + 1;

If (* iter_l> = '0' & * iter_l <= '9 '&&
* ITER> = '0' & * ITER <= '9 '&&
* Iter! = * Iter_l)
{/*-Number ***************
*********************/
If (* ITER <* iter_l) {/* ascending order 0-9 */
-- Ds;
For (temp = * ITER; temp <* iter_l; temp ++ ){
* (DS ++) = temp;
}
Continue;
}
/* 9-0 in descending order */
-- Ds;
For (temp = * ITER; temp> * iter_l; temp --){
* (DS ++) = temp;
}
Continue;
}/* End with a number *************
**********************/
Else if (* iter_l> = 'A' & * iter_l <= 'Z '&&
* ITER> = 'A' & * ITER <= 'Z '&&
* Iter! = * Iter_l)
{/*-Lowercase letter **********
********************/
If (* ITER <* iter_l) {/* ascending a-z */
-- Ds;
For (temp = * ITER; temp <* iter_l; temp ++ ){
* (DS ++) = temp;
}
Continue;
}
/* Descending Z-*/
-- Ds;
For (temp = * ITER; temp> * iter_l; temp --){
* (DS ++) = temp;
}
Continue;
}/****** Lowercase letter ending ****
**********************/

Else if (* iter_l> = 'A' & * iter_l <= 'Z '&&
* ITER> = 'A' & * ITER <= 'Z '&&
* Iter! = * Iter_l)
{/*-UPPERCASE letters **********
********************/
If (* ITER <* iter_l) {/* ascending A-Z */
-- Ds;
For (temp = * ITER; temp <* iter_l; temp ++ ){
* (DS ++) = temp;
}
Continue;
}
/* Z-A in descending order */
-- Ds;
For (temp = * ITER; temp> * iter_l; temp --){
* (DS ++) = temp;
}
Continue;
}/***** End with uppercase letters *****
*********************/

If neither else {/* is available, copy -*/
* (DS ++) = '-';
}

}/* If ends */
Else {/* Copy as is */
* (DS ++) = * ss;
}
}/* For end */

/* Add '/0' to the last position of DS '*/
* DS = '/0 ';

Return ds_head;
}
/*
Although a program in this function can be written in inline mode, it is not written because of the waste of parameter passing. Therefore, it is a bit redundant.
*/
Int main (void)
{
Char A [100000] = "-------- a-zz-aA-ZZ-A0-99-09-aa-b -------- ";
Char B [100000];
Char * C;
Printf ("1: % s/n", );
C = expand (A, B );
Printf ("2: % s/n", C );
Printf ("3: % s/n", expand (a, B ));
 
System ("pause ");

Return 0;
}

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.