A good string transcoding and decoding function (self-writing), string Function

Source: Internet
Author: User

A good string transcoding and decoding function (self-writing), string Function

function isString(variable) {  return Object.prototype.toString.call(variable).indexOf('String') != -1;}function isNumeric(variable) {  return !isNaN(parseFloat(variable)) && isFinite(variable);}function stringEncode(string) {  string = isString(string) || isNumeric(string) ? String(string) : '';  var code,    i = 0,    code_string = '',    len = string.length;  while(i < string.length) {    code = string.charCodeAt(i);    code_string += '' + String(code).length + code;    i++;  }  return code_string;}function stringDecode(code) {  var i = 0,    code_len,    decode_string = '';  code = String(code);  while(i < code.length) {    code_len = +code.charAt(i);    i++;    decode_string += String.fromCharCode(+code.substr(i, code_len));    i += code_len;  }  return decode_string;}

Write a function to copy string 2 to string 1. The strcpy function is not allowed;

Strlen (char * p) and strcmp (char * p1, char * p2):]

(1) String Length function:

Int strlen (char * p)
{
Int I = 0;
While (p [I]! = '\ 0 '){
I ++;
}
Return I;

}

(2) string replication functions:

// Assign all content indicated by p2 to p1
Void strcpy (char * p1, char * p2)
{
Int I = 0;
While (p2 [I]! = '\ 0 '){
P1 [I] = p2 [I];
I ++;
}
P1 [I] = '\ 0 ';

}

(3) string comparison functions:

Int strcmp (char * p1, char * p2)
{
Int I = 0;
While (p1 [I]! = '\ 0' & p2 [I]! = '\ 0 '){
If (p1 [I]-p2 [I])
Return p1 [I]-p2 [I];
I ++;

}
Return strlen (p1)-strlen (p2 );

}

(4) Finally, provide a main function for you to test, and attach the test cases and expected output results:]

Int main ()
{
Char * p1, * p2;
P1 = new char [1000];
P2 = new char [1000];
While (cin> p1> p2 ){

Cout <strlen (p1) <endl;
Cout <strlen (p2) <endl;
Cout <strcmp (p1, p2) <endl;

Strcpy (p1, p2 );

Cout <"after copying p1 becomes"
<P1 <endl;

}

Return 0;
}
/*
HelloWorld
MyGirl
10
6
-5
After copying p1 becomes myGirl

Abcdef
Bcdefg
6
6
-1
After copying p1 becomes bcdefg

Abcdefg
... The remaining full text>

Compile a function to convert an integer to a numeric string. C Language

# Include <stdio. h> # define N 10
// Compile a function to extract the numbers of All integers and convert them into numeric strings,
// Output the string in the main function. Use a simple C language without a pointer.
Int main ()
{
Int number_int, I, str_len;
Char number_str [N], swap_temp;
Scanf ("% d", & number_int );
I = 0;
While (number_int)
{
Number_str [I] = number_int % 10 + 48;
Number_int/= 10;
I ++;
}
Number_str [I] = '\ 0 ';
Str_len = I-1;
For (I = 0; I <= str_len/2; I ++)
{
Swap_temp = number_str [I];
Number_str [I] = number_str [str_len-i];
Number_str [str_len-i] = swap_temp;
}
Printf ("% s \ n", number_str );

}

Related Article

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.