C: 2-8

Source: Internet
Author: User

Lab 1:

# Define Yes 1

# Define no 0

/* Htoi: Convert hexadecimal string s to integer */

Int htoi (char s [])

{

Int hexdigit, I, j, inhex, N;

I = 0;

If (s [I] = '0') {// skip optional 0x or 0x

++ I;

If (s [I] = 'X' | s [I] = 'X ')

++ I;

}

N = 0; // intager value to be returned

Inhex = yes; // assume valid hexadecimal digit

For (; inhex = Yes; ++ I ){

If (s [I]> = '0' & S [I] <= 'F ')

Hexdigit = s [I]-'0 ';

Else if (s [I]> = 'A' & S [I] <= 'F ')

Hexdigit = s [I]-'A' + 10;

Else if (s [I]> = 'A' & S [I] <= 'F ')

Hexdigit = s [I]-'A' + 10;

Else

Inhex = no; // not a valid hexadecimal digit

If (inhex = Yes)

N = 16 * n + hexdigit;

}

Return N;

}

Converts a hexadecimal string to an equivalent integer.

 

Lab 2:

The squeeze (S1, S2) function deletes any character in string S1 that matches the character in string S2.

/* Squeeze: delete each char in S1 which is in S2 */

Void squeeze (char S1 [], char S2 [])

{

Int I, J, K;

For (I = k = 0; S1 [I]! = '/0'; I ++ ){

For (j = 0; S2 [J]! = '/0' & S2 [J]! = S1 [I]; j ++)

;

If (s2 [J] = '/0') // end of string-No match

S1 [k ++] = S1 [I];

}

S1 [k] = '/0 ';

}

 

Lab 3:

/* Any: return first location in S1 where any char from S2 occurs */

Int any (char S1 [], char S2 [])

{

Int I, J;

For (I = 0; S1 [I]! = '/0'; I ++)

For (j = 0; S2 [J]! = '/0'; j ++)

If (S1 [I] = S2 [J]) // match found?

Return I; // location first match

Return-1; // otherwise, no match

}

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.