c
我从不认识王先生
这个作者很懒,什么都没留下…
展开
-
c 时间格式化 yyyy-MM-dd HH:mm:ss
void show_time(int sig){ time_t times = time(NULL); struct tm *utcTime = gmtime(×); char time_format[30]; sprintf(time_format , "%d-%02d-%02d %02d:%02d:%02d",utcTime->tm_year+1900, utcTime->tm_mon+1, utcTime->tm_mday, utc原创 2021-09-23 23:53:35 · 1184 阅读 · 0 评论 -
c语言调用 java
#include <stdio.h>#include <unistd.h>#include <string.h>#include <errno.h>#include <stdlib.h>typedef struct RuntimeException{ char *msg; int code;} RuntimeException;RuntimeException* exception(int code , char原创 2021-09-19 11:51:46 · 920 阅读 · 0 评论 -
strerror 的使用方式
#define _CRT_SECURE_NO_WARNINGS 1;#include <stdio.h>#include <string.h>#include <errno.h>/** strerror(int errnum) 返回错误码所对应的错误信息 c 库函数执行时可能返回的错误 0 - 43 */int main() { int i = 0; for (i = 0; i < 44; i++) { char* str = str原创 2021-05-26 22:32:17 · 127 阅读 · 0 评论 -
strtok 函数的使用方式
#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>#include <string.h>/** 字符串分割*/int main() { char str[] = "ssh [email protected]"; char* sep = " @."; char* current = NULL; for (current = strtok(str, sep); current != NULL; current =原创 2021-05-26 22:24:35 · 49 阅读 · 0 评论 -
自己实现一个 strstr (字符串寻找子串)
#include <stdio.h>#include <assert.h>char* my_strstr(const char* p1 , const char* p2) { assert(p1 != NULL); assert(p2 != NULL); char* s1 = p1; char* s2 = p2; char* current = p1; if (*p2 == '\0') { return p1; } while (*current)原创 2021-05-26 21:23:34 · 64 阅读 · 0 评论 -
自己实现一个 strncmp (字符串限长比较)
#include <stdio.h>#include <assert.h>int my_strncmp(const char* str1, const char* str2 , int count) { assert(str1 && str2); while (count-- && *str1 != '\0' && *str2 != '\0') { if (*str1++ != *str2++) { return原创 2021-05-24 23:31:15 · 176 阅读 · 0 评论 -
自己实现一个 strncat (限长字符串拼接)
#include <stdio.h>char* myStrncat(char* target ,const char* source , int count) { char* start = target; //找到 '\0' while (*target++); //去掉 '\0' target--; while (count--) { //拼接 if (!(*target++ = *source++)) { return start; } }原创 2021-05-23 11:07:15 · 111 阅读 · 0 评论 -
自己实现一个 strncopy (字符串限长复制)
#include <stdio.h>char* my_strncopy(char* target , const char* source , int count) { char* dest = target; while (count && (*target++ = *source++)) { count--; } if (count) { while (--count) { dest += '\0'; } } return dest;原创 2021-05-23 10:48:55 · 275 阅读 · 0 评论 -
自己实现一个 strcmp (字符串比较)
1:第一个字符串大于第二个字符串,返回 > 0 的数字2:两个字符串相等,返回 03:第一个字符串小于第二个字符串,返回 < 0 的数字#include <stdio.h>#include <assert.h>int my_strcmp(const char* source , const char* target) { assert(source && target); while (*source == *target) {原创 2021-05-23 10:02:57 · 147 阅读 · 0 评论 -
自己实现一个 strcat (字符串追加)
1:源字符必须包含 ‘\0’2:源字符串数组空间要够大3:目标空间必须可修改#include <stdio.h>#include <string.h>char* my_strcat(char* source , const char* target) { char* dest = source; int len = strlen(source); source += len; while (*source++ = *target++) { ; }原创 2021-05-22 18:33:01 · 198 阅读 · 0 评论 -
自己实现一个 strcopy (字符串复制)
#include <stdio.h>#include <assert.h>#include <string.h>void myStrcopy(char * source , const char * target) { assert(source != NULL); assert(target != NULL); assert(strlen(source) >= strlen(target)); while ((*source++=*target+原创 2021-05-22 16:25:09 · 561 阅读 · 0 评论 -
自己实现一个 strlen 函数(获取字符串长度)
#include <stdio.h>int myStrlen(const char * str) { int i = 0; while (*str != '\0') { str++; i++; } return i; }int main() { printf("%d \n" , myStrlen("1")); return 0;}原创 2021-05-22 16:00:32 · 216 阅读 · 0 评论 -
杨氏矩阵
#include <stdio.h>/** 杨氏矩阵 - 剑指 offer 题目内容: 有一个数字矩阵,矩阵的每行从左到右是递增的,矩阵从上到下是递增的,请编写程序在这样的矩阵中查找某个数字是否存在 要求: 事件复杂程度小于 O(N)*/int main() { int arr[3][3] = { {1,2,3},{2,3,4},{3,4,5} }; int res = findNumber(arr , 4 ,3 , 3); if (res) printf(原创 2021-05-22 15:25:56 · 41 阅读 · 0 评论 -
判断字符串 efabcd 是 abcdef 旋转得来的
#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h>#include <string.h>/** 判断字符串 efabcd 是 abcdef 旋转得来的*/int main() { char source[30] = "abcdef"; char target[] = "efabcd"; int res = is_move(source,target); if (res) printf("Yes");原创 2021-05-22 12:53:02 · 143 阅读 · 0 评论 -
杨辉三角
杨辉三角#include <stdio.h>int main() { int arr[10][10] = { 0 }; int i = 0; int j = 0; for (i = 0; i < 10; i++) { for (j = 0; j < 10; j++) { if (j == 0) { arr[i][j] = 1; } if (i == j) { arr[i][j] = 1; } if (i >原创 2021-05-22 10:18:00 · 63 阅读 · 0 评论 -
运动员名次问题
5位运动员A -> B第二,我第三B -> 我第二,E第四C -> 我第一,D第二D -> C最后,我第三E -> 我第四,A第一这几个运动员每人说的话只有一半是对的。#include <stdio.h>int main() { int a = 0; int b = 0; int c = 0; int d = 0; int e = 0; for (a = 1; a <= 5; a++) { for (b =原创 2021-05-22 10:22:54 · 82 阅读 · 0 评论 -
旋转同一块内存中的元素
题目内容: 实现一个函数,可以左旋字符串中的 k 个字符 例如: ABCD 左旋一个字符得到 BCDA ABCD 左旋一个字符得到 CDAB#include <stdio.h>#include <string.h>void swap(char* arr, int start, int end) { int left = 0; int right = 0; //dichotomy swap int mid = (start + end) / 2; for原创 2021-05-22 10:26:17 · 60 阅读 · 0 评论