Programming questions
1. Create a single-join table. Insert the 26 letters 'A' -- 'Z' into the linked list and print them in reverse order!
2. Write a function. Its prototype is
Int continumax (char * outputstr, char * intputstr );
Function: Find the longest consecutive numeric string in the string, return the length of the string, and pay the longest numeric string to the memory specified by one of the function parameters outputstr. For example, after the first address of "abcd12345ed125ss123456789" is passed to intputstr, the function returns 9 and the Value indicated by outputstr is 123456789;
3. Write your own atoi functions;
1,
# Include <stdio. h> # include <malloc. h> struct node {int data; struct node * Next;}; struct node * head; void singlelinklistcreate () {head = (struct node *) malloc (sizeof (struct node); head-> next = NULL;} void singlelinklistadd (INT data, int I) {struct node * PTR = (struct node *) malloc (sizeof (struct node); struct node * P = head; while (I --) {If (p-> next = NULL) Exit (-1 ); P = p-> next;} PTR-> next = p-> next; P-> next = PTR; PTR-> DATA = data;} void singlelinklistdelete (int I) {struct node * P = head; struct node * TMP; while (I --) {If (p-> next = NULL) Exit (-1 ); P = p-> next;} TMP = p-> next; P-> next = TMP-> next; free (TMP);} void singlelinklistshow () {struct node * PTR = head; while (PTR-> next) {PTR = PTR-> next; printf ("% C \ t", PTR-> data );}} void reverse () {struct node * PTR; struct node * pre; struct node * next; if (Head-> next = NULL | head-> next = NULL) return; Pre = head-> next; PTR = pre-> next; next = PTR-> next; pre-> next = NULL; while (next) {PTR-> next = pre; Pre = PTR; PTR = next; next = Next-> next;} PTR-> next = pre; head-> next = PTR;} int main () {int I; singlelinklistcreate (); for (I = 0; I <26; I ++) {singlelinklistadd (I + 'A', I);} printf ("original string: \ n "); singlelinklistshow (); reverse (); printf ("\ n after reverse conversion: \ n"); singlelinklistshow (); Return 0 ;}
2,
# Include <stdio. h> # include <malloc. h> int continumax (char * outputstr, char * inputstr) {int prelength = 0; char * pretail = inputstr; int length = 0; char * head = inputstr; char * pstr = inputstr; int head_finded = 0; int * dststraddr = outputstr; while (* pstr! = '\ 0') {If (head_finded = 0) {If (* pstr> = '0' & * pstr <= '9') {head = pstr; head_finded = 1; length = 1 ;}} else {If (* pstr> = '0' & * pstr <= '9') {length ++ ;} else {If (length> prelength) {outputstr = head; prelength = length; pretail = pstr;} head_finded = 0 ;}} pstr ++;} If (length> prelength) {outputstr = head; prelength = length; pretail = pstr;} * dststraddr = outputstr; return length;} int main (void) {char * srcstr = "abcd12345ed125ss123456789 "; char * dststr; dststr = & dststr; int length = continumax (dststr, srcstr); printf ("Longest digit string length: % d \ n longest digit string: \ n ", length); While (length --) {printf ("% C", * dststr ++);} return 0 ;}
In this question, we encountered a difficulty, that is, "outputstr refers to 123456789;", because the function prototype is "Int continumax (char * outputstr, char * intputstr ); ", so the question is to modify the address saved by the pointer under the condition that the pointer is used as a function parameter. However, we all know that the change of the shape parameter is only within the function, the real parameter is not changed after the function call is completed. Generally, the method to solve this problem is called "transfer address". When applied to this question, it should be to change the function prototype to "int continumax (char ** outputstr, char * intputstr); ", apparently in conflict with the meaning of the question. Think about it without any solution. With the above solution: Assign the pointer address to yourself! In this way, you can modify the parameters.
3,
#include <stdio.h>int my_atoi(const char *nptr){ int result = 0; int flag = 1; if (*nptr == '-') { flag = -1; nptr++; } else if (*nptr == '+') { nptr++; } while (*nptr != '\0') { if (*nptr > '9' || *nptr < '0') { break; } result = result * 10 + *nptr -'0'; nptr++; } return result * flag;}int main(){ char *str = "+124324ab"; char *str2 = "-1234.567891"; printf("%d\n", my_atoi(str)); printf("%f\n", my_atof(str2)); printf("%d",atoi("-4afsf12r4-345f")); return 0;}int main(){ char *str = "+124324ab"; char *str2 = "-1234.567891"; printf("%d\n", my_atoi(str)); return 0;}