当前位置:网站首页>Exercise analysis summary
Exercise analysis summary
2022-06-23 01:43:00 【'Dream_】
If you will It's better to start than to give up . Roman.
May we all have our own goals and are making unremitting efforts for them .
-----------------------------------------------------------------------
1、 Implement case conversion
// Implement case conversion
// utilize getchar Realization : Be careful Cache space
#include<stdio.h>
int main()
{
char ch = 0;
printf(" Please enter :\n");
while ((ch = getchar()) != EOF)
{
if ((ch >= 'a') && (ch <= 'z'))// Small to capital
{
ch -= 32;
}
else if ((ch >= 'A') && (ch <= 'Z')) // Capitalize to lowercase
{
ch += 32;
}
else
{
printf(" illegal input !\n");
continue;
}
getchar();// Clear cache
printf(" The result of the conversion is :\n");
printf("%c\n", ch);
}
return 0;
}***********************************************************
2、 String inversion ( Don't use library functions Not in reverse order )
// String inversion ( Don't use library functions Not in reverse order )
#include<stdio.h>
// Calculate string length
int getlen(char ch[])
{
int count = 0;
while (*ch != '\0')
{
count++;
ch++;
}
return count;
}
// Method 1 : Non recursive implementation of inverse : Subscript
void Fturn1(char ch[], int len)
{
int left = 0;
int right = len - 1;
while (left < right) // Attention condition
{
char tmp = ch[left];
ch[left] = ch[right];
ch[right] = tmp;
left++;
right--;
}
printf(" Method 1 : Non recursive ( Subscript ) Realize inversion :\n");
printf("%s\n", ch);// Note that the printing method does not require i Loop printing
}
// Note the pointer method !!
// Method 1 : Non recursive implementation of inverse : The pointer
void Fturn2(char* ch, int len)
{
char* left = ch;
char* right = ch + len - 1;
while (left < right)
{
char tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
}
printf(" Method 1 : Non recursive ( The pointer ) Realize inversion :\n");
printf("%s\n", ch);// Note that the printing method does not require i Loop printing
}
// Method 2 : Recursive implementation of inverse : assignment \0 Law
// Means of implementation : Swap heads and tails +turn( A string of characters in the middle )
// Pay attention to this method !!!
void turn(char* ch, int len)
{
char tmp = *ch;
*(ch) = *(ch+len-1);
*(ch + len - 1) = '\0';
// Recursion must have constraints : The number in the middle is greater than or equal to 2
len = (getlen(ch + 1));
if (len >= 2)
{
turn(ch+1, len);
}
*(ch + len + 2 - 1) = tmp; // Notice here !!!: Be sure to add. 2
}
int main()
{
// Define a string
char ch[] = "abcdefgh";
printf(" Original string :%s\n", ch);
// Calculate string length
int len = getlen(ch);
// Inversion : First and last characters are exchanged , Exchange in turn
// Print in reverse order : Just print in reverse
// Method 1 : Non recursive implementation of inverse
Fturn1(ch, len);// Subscript
char ch1[] = "abcdefgh";
Fturn2(ch1, len);// The pointer
// Method 2 : Recursive implementation of inverse
char ch2[] = "abcdefgh";
turn(ch2, len);
printf(" Method 2 : Recursive implementation of inverse :\n");
printf("%s\n", ch2);// Note that the printing method does not require i Loop printing
return 0;
}Be careful :
- Within the function strlen You can find the length of the string , however sizeof Can not be
- notes :strlen Record only '\0' The number of characters before , however sizeof hold '\0' Also counted as characters , Calculate the length
- Note the use of recursion in the code part
***********************************************************
3、 Recursive implementation n Of k Power
// Recursive implementation n Of k Power
#include<stdio.h>
double Power(int n, int k)
{
// Three situations :k Greater than 0 be equal to 0 Less than 0
if (0 == k)
{
return 1.0;
}
else if (k > 0)
{
return n * Power(n, k-1);
}
else
{
return (1.0 / Power(n, -k));// Note that -k
}
}
int main()
{
int n = 0;
int k = 0;
printf(" Please input the base number n And the index k:\n");
scanf("%d %d", &n, &k);
double put = Power(n, k);
printf("%d Of %d The power result is :%lf\n", n, k, put); // Be careful double The corresponding is %lf
return 0;
}***********************************************************
4、 distinguish The length of the array And String length
- String length strlen Yes. '\0' , Record the previous number of characters , encounter '\0' To stop counting
- Be careful strlen Acting on strings , Not an integer array
- example :
char acX[] = “abcdefg”; char acY[] = {‘a’,’b’,’c’,’d’,’e’,’f’,’g’};
- The length of the array : about acX In terms of arrays : a b c d e f g \0 common 8 Character length
- about acY In terms of arrays : a b c d e f g common 7 Character length
- String length : strlen Calculation
- about acX In terms of arrays : a b c d e f g \0 common 7 Character length ( Calculation \0 Number of previous characters )
- about acY In terms of arrays : a b c d e f g It didn't appear after \0 , namely : Followed by random values , When will it appear \0 Unknown , That is, the string length is unknown
***********************************************************
5、 Function pursuit High cohesion and low coupling : Functions should be as independent as possible , Single function
--------------------------- All a person's anger comes from the pain of his incompetence .---------------------------
边栏推荐
- [luogu] p1083 [noip2012 improvement group] borrow classroom (line segment tree)
- Autumn move script a
- Xiaobai operates win10 to expand Disk C (allocate disk D memory to Disk C) and the test is valid for many times
- Extend your kubernetes API using the aggregation API
- There is no corresponding change on the page after the code runs at the Chrome browser break point
- [hdu] P7079 Pty loves lines
- Day575: divide candy
- 7.new, delete, OOP, this pointer
- 3D printing microstructure
- Template specialization template <>
猜你喜欢

E-R diagram

1. introduction to MySQL database connection pool function technology points

MySQL -- how to access the database of a computer in the same LAN (prenatal education level teaching)

On AI and its future trend | community essay solicitation
![[hdu] P6964 I love counting](/img/ff/f8e79d28758c9bd3019816c8f46723.png)
[hdu] P6964 I love counting

Debian10 LVM logical volumes

JS - single sign on
![[Title Fine brushing] 2023 Hesai FPGA](/img/e8/d9d8c549a4fee529b69ebfc9a9d6ef.png)
[Title Fine brushing] 2023 Hesai FPGA

Local deployment and problem solving of IIS in ArcGIS JS 4.23

6. const usage, combination of first and second level pointers
随机推荐
Day500: keyboard line
3D打印微组织
3. compilation and linking principle
Real topic of the 2020 Landbridge cup provincial competition - go square (dp/dfs)
leetcode 91. Decode Ways 解码方法(中等)
2D prefix and
[ZOJ] P3228 Searching the String
C serializabledictionary serialization / deserialization
On AI and its future trend | community essay solicitation
Day260: the number III that appears only once
Template specialization template <>
JS prototype and prototype chain Paramecium can understand
Analysis of current mainstream video coding technology | community essay solicitation
The devil cold rice # 099 the devil said to travel to the West; The nature of the boss; Answer the midlife crisis again; Specialty selection
Unit of RMB in words
Bc117 xiaolele walks up the steps
Installing MySQL for Linux
leetcode 91. Decode ways (medium)
Philosopher's walk gym divide and conquer + fractal
Bc113 small leloding alarm clock