1、#include <stdio.h>
#include <string.h>
//先去掉前面的空格,再去掉后面的空格
int trimSpace(char *inbuf, char *outbuf)
{
char *p = inbuf;
//查看有多少个空格
int len = 0;
while(p[len] == ' ')
{
len++;
}
//将字符串进行前移
int i = len;
while(p[i])
{
p[i - len] = p[i];
i++;
}
p[i - len] = '\0';
//去掉后面的字符串
for(int j = 0; j < strlen(p); j++)
{
if(p[j] == ' '){
p[j] = '\0';
outbuf = p;
return 1;
}
}
return 0;
}
int main()
{
char str[] = " abcdefgdddd ";
char *outbuf = NULL;
if(trimSpace(str, outbuf))
{
printf("start.%s.end", str);
}else
{
printf("No need for trim.");
}
}
############################################################
2、#include <stdio.h>
#include <string.h>
//输入字符串,输出两个截取之后的字符串
int getStr1Str2(char *str, char *buf1, char *buf2){
char *p = str;
int temp1 = 0;
int temp2 = 0;
if(p == NULL)
{
return -1;
}
for(int i = 0; i < strlen(p); i++)
{
if(i % 2 == 0){
buf1[temp1] = p[i];
temp1++;
}else{
buf2[temp2] = p[i];
temp2++;
}
}
return 1;
}
int main()
{
char str[] = "1a2b3d4z";
char buf1[10] = {0};
char buf2[10] = {0};
if(getStr1Str2(str, buf1, buf2)){
printf("buf1 = %s, buf2 = %s\n", buf1, buf2);
}else{
printf("Error!");
}
}
###########################################################
3、#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//先去掉前面的空格,再去掉后面的空格
int trimSpace(char *inbuf){
char *p = inbuf;
//查看有多少个空格
int len = 0;
while(p[len] == ' '){
len++;
}
//将字符串进行前移
int i = len;
while(p[i]){
p[i - len] = p[i];
i++;
}
p[i - len] = '\0';
//去掉后面的字符串
for(int j = 0; j < strlen(p); j++){
if(p[j] == ' '){
p[j] = '\0';
return 1;
}
}
return 0;
}
//输入字符串,输出key和value
int getValueByKey(char *str, char *keybuf, char *valuebuf){
char *p = str;
char *tempKeyBuf = keybuf;
char *tempValueBuf = valuebuf;
if(p == NULL){
return 0;
}
int pos = 0;
//首先确认等号的位置,将字符串拆分成两个部分
for(int i = 0; i < strlen(p); i++){
if(p[i] == '='){
pos = i;
break;
}
}
strncpy(tempKeyBuf, p, pos);
strncpy(tempValueBuf, p + pos + 1, strlen(p) - pos);
trimSpace(tempKeyBuf);
trimSpace(tempValueBuf);
return 1;
}
int main(){
char str1[] = "key2= valude2 ";
char keybuf[100] = {0};
char valuebuf[100] = {0};
if(getValueByKey(str1, keybuf, valuebuf)){
printf("key = %s, value = %s\n", keybuf, valuebuf);
}else{
printf("ERROR.\n");
}
return 0;
}
################################################################
4、
a)、#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void print_string(const char* str)
{
int len = strlen(str);
for (int i = 0; i < len; i++)
{
if (str[i] == 0)
break;
printf("%c ", str[i]);
}
}
int main()
{
const char* a = "abcd";
print_string(a);
return 0;
}
b)、#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void translate(const char* str)
{
const char* p = str;
while (*p)
{
char ch = *p;
if (*p != '/')
{
printf("%c", ch);
}
else
{
p++;
ch = *p;
if (ch=='s')
{
printf("^_^");
}
else if (ch == 'f')
{
printf("@_@");
}
else if (ch =='c')
{
printf("T_T");
}
else
{
printf("/%c", ch);
}
}
p++;
}
}
int main()
{
const char* a = "Thank you/s I will try my best/c";
translate(a);
return 0;
}
#####################################################################
5、
a)、#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void compare(const char* str)
{
//调用字符串比较函数,strcmp,当两字符串相同时,值为0。
if (strcmp(str,"jan")==0
||strcmp(str,"january") ==0
||strcmp(str,"1") == 0)
{
printf("你输入正确");
}
else
{
printf("你输入错误");
}
}
char get_input()
{
char a[16];
printf("你生日的月份是?\n");
gets_s(a);
compare(a);
return 0;
}
int main()
{
get_input();
return 0;
}
b)、#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Object
{
char value[32];
};
void select_sort(char* arr[], int n)
{
for (int i = 0; i < n-1; i++)
{
for (int j = i+1; j < n; j++)
{
if (strcmp(arr[j], arr[i]) < 0)
{
char* temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
}
int main()
{
int i;
Object objs[5];
for (i = 0; i < 5; i++)
{
printf("please input:");
gets_s(objs[i].value);
}
printf("over input.\n");
char* name[5];
for (i = 0; i < 5; i++)
{
name[i] = objs[i].value;
}
select_sort(name, 5);
for (i = 0; i < 5; i++)
{
printf("%s \n", name[i]);
}
return 0;
}
################################################################
6、#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void to_string(const int* arr,int size,char* output)
{
char buf[128];
int count = 0;
for (int i = 0; i < size; i++)
{
sprintf_s(buf, "%d,", arr[i]);
for (int j = 0; j < buf[j]; j++)
{
output[count] = buf[j];
count++;
}
}
output[count] = 0;
}
int main()
{
int arr[4] = { 18,987,1235,-911 };
char buf[512];
to_string(arr, 4, buf);
int len = strlen(buf);
for (int i = 0; i < len; i++)
{
printf("%c", buf[i]);
}
printf("\n");
return 0;
}
################################################################
7、
a)、#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* AfTrim(char* src)
{
int len = strlen(src);
int start = 0; //copy字符串的开头引号
int end = 0; // copy字符串的结尾引号
int i = 0; //从头遍历的引号
int k = len-1; // 从尾遍历的引号
char* copy = (char*)malloc(len + 1); //申请一片相同大小的内存
while (src[i]) //从头遍历,直至找到不为空格终止
{
if (src[i] == ' ')
i++;
else
{
start = i;
break;
}
}
while (src[k]) //从尾遍历,直至找到不为空格终止
{
if (src[k] == ' ')
{
k--;
}
else
{
end = k+1;
break;
}
}
//进行copy
for (int j = start; j < end; j++)
{
copy[j-start] = src[j];
printf("%c", src[j]);
}
return copy;
free(copy);
}
int main()
{
char a[] = " i am fine ";
AfTrim(a);
return 0;
}
b)、#include <stdio.h>
#include <string.h>
#inc