Summary of embedded written test questions

Source: Internet
Author: User
Tags define null
Summary of embedded written test questions

1. Sort a string in reverse order

# Include <iostream> <br/> using namespace STD; <br/> // # define null (void *) 0) <br/> char * mystrrev (char * const DEST, const char * const SRC) <br/>{< br/> If (DEST = NULL & src = NULL) <br/> return NULL; <br/> char * ADDR = DEST; <br/> int val_len = strlen (SRC); <br/> Dest [val_len] = '/0'; <br/> int I; <br/> for (I = 0; I <val_len; I ++) <br/> {<br/> * (DEST + I) = * (SRC + val_len-i-1); <br/>}< br/> return ADDR; </P> <p >}< br/> main () <br/>{< br/> char * STR = "asdfa"; <br/> char * str1 = NULL; <br/> str1 = (char *) malloc (20); <br/> If (str1 = NULL) <br/> cout <"malloc failed"; <br/> cout <mystrrev (str1, str); <br/> free (str1); <br/> str1 = NULL; // eliminates wild pointers <br/>}
2. Sort a linked list in reverse order

# Include <stdio. h> <br/> # include <ctype. h> <br/> # include <stdlib. h> <br/> typedef struct list {<br/> int data; <br/> struct list * Next; <br/>} List; <br/> list * list_create (void) <br/>{< br/> struct list * head, * tail, * P; <br/> int e; <br/> head = (list *) malloc (sizeof (list); <br/> tail = head; <br/> printf ("/NLIST create, input numbers (end of 0): "); <br/> scanf (" % d ", & E); <br/> while (E) {<br/> P = (list *) malloc (sizeof (List); <br/> P-> DATA = E; <br/> tail-> next = P; <br/> tail = P; <br/> scanf ("% d", & E) ;}< br/> tail-> next = NULL; <br/> return head; <br/>}< br/> list * list_reverse (list * head) // core code <br/>{< br/> list * P, * q, * R; <br/> P = head; <br/> q = p-> next; <br/> while (Q! = NULL) <br/>{< br/> r = Q-> next; <br/> q-> next = P; <br/> P = Q; <br/> q = r; <br/>}< br/> head-> next = NULL; <br/> head = P; <br/> return head; <br/>}< br/> void main (void) <br/>{< br/> struct list * head, * P; <br/> int D; <br/> head = list_create (); <br/> printf ("/N"); <br/> for (P = head-> next; P; P = p-> next) <br/> printf ("-- % d --", p-> data); <br/> head = list_reverse (head ); <br/> printf ("/N"); <br/> for (P = head; P-> next; P = p-> next) <br/> printf ("-- % d --", p-> data); <br/>}
3. Calculate the number of bits in a byte to be set to 1.

# Receivede <iostream> <br/> # define N 10 <br/> // defines the byte type alias <br/> # ifndef byte <br/> typedef unsigned char byte; <br/> # endif <br/> int comb (byte B [], int N) <br/> {<br/> int COUNT = 0; <br/> int Bi, BJ; <br/> byte cc = 1, TT; <br/> // obtain the byte data from the first bi record <br/> for (Bi = 0; Bi <n; Bi ++) <br/>{< br/> // calculate the number of 1 in the eight bits of the byte. <br/> TT = B [bi]; <br/> for (bj = 0; BJ <8; BJ ++) <br/> {<br/> // is the result of Phase 1 or Mode 2 1 1 1? Test whether the current BIT is 1 <br/> // If (TT % 2 = 1) <br/> If (TT & CC) = 1) <br/>{< br/> count ++; <br/>}< br/> // shifts one or two places to the right, same effect <br/> // TT = tt> 1; <br/> TT = TT/2; <br/>}< br/> return count; <br/>}< br/> // test <br/> int main () <br/> {<br/> byte B [10] = {,}; <br/> cout <comb (B, n) <Endl; <br/> return 0; <br/>}
4. Search for a given byte)
5. Find the longest possible substring in a string

Char * search (char * cpsource, char ch) <br/>{< br/> char * cptemp = NULL, * cpdest = NULL; <br/> int itemp, icount = 0; <br/> while (* cpsource) <br/> {<br/> If (* cpsource = CH) <br/> {<br/> itemp = 0; <br/> cptemp = cpsource; <br/> while (* cpsource = CH) <br/> ++ itemp, ++ cpsource; <br/> If (itemp> icount) <br/> icount = itemp, cpdest = cptemp; <br/> If (! * Cpsource) <br/> break; <br/>}< br/> + + cpsource; <br/>}< br/> return cpdest; <br/>}
6. convert a string to an integer

Int myatoi (char STR []) <br/>{< br/> int I; <br/> int Weight = 1; // weight <br/> int RTN = 0; // return value <br/> for (I = strlen (STR)-1; I> = 0; I --) <br/> {<br/> RTN + = (STR [I]-'0') * weight; // <br/> weight * = 10; // weight gain <br/>}< br/> return RTN; <br/>}< br/> void main () <br/>{< br/> char STR [32]; <br/> printf ("input a string:"); <br/> gets (STR ); <br/> printf ("% d/N", myatoi (STR); <br/>}
7. Convert Integers to strings

# Include <stdio. h> <br/> # include <string. h> <br/> void reverse (char s []) <br/> {// string inversion <br/> int C, I = 0, J; <br/> for (j = strlen (S)-1; I <j; j --) <br/> {c = s [I]; <br/> S [I] = s [J]; <br/> S [J] = C; <br/> I ++; <br/>}< br/> void integertostring (char s [], int N) <br/> {int I = 0, sign; <br/> If (Sign = N) <0) // if it is a negative number, first convert it to a positive number <br/> N =-N; <br/> do // changes from a single bit to a character until the maximum bit. <br/> {s [I ++] = n % 10 + '0' should be reversed '; <br/>}while (n = N/10)> 0); <br/> // if it is a negative number, add a negative number. <br/> If (sign <0) <br/> S [I ++] = '-'; <br/> S [I] = '/0 '; // string ends <br/> reverse (s); <br/>}< br/> void main () <br/> {int m; <br/> char C [100]; <br/> printf ("enter an integer m:"); <br/> scanf ("% d", & M ); <br/> integertostring (C, M); <br/> printf ("integer = % d string = % s/n", M, c); <br/>}

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.