//字符串翻转
char* strReverse(char* str)
{
char* start=str;
char* end=str+strlen(str)-1;
char temp;
if(str!=NULL)
{
while(start<end)
{
temp = *start;
*start++ = *end;
*end-- = temp;
}
}
return str;
}
void main()
{
char str[] = "This is a string!";
int len = strlen(str);
strReverse(str);
printf("%s\n", str);
system("pause");
}
**************************************************我是分隔线*********************************************************
//字符串拷贝
char * strcpy( char *strDest, const char *strSrc )
{
assert( (strDest != NULL) &&(strSrc != NULL) );
char *address = strDest;
while( (*strDest++ = * strSrc++) != ‘\0’ );
return address;
}
**************************************************我是分隔线*********************************************************
#include <stdio.h>
#include <stdlib.h>
int strcmp(char *source, char *dest)
{
while(*source == *dest && *source != '\0' && *dest != '\0')
{
source++;
dest++;
}
if (*source =='\0' && *dest == '\0')
return 0;
else
return -1;
}
int main()
{
char *str1 = "abcde";
char *str2 = "abcde";
printf("ret = %d", mystrcmp(str1, str2));
return 0;
}
**************************************************我是分隔线*********************************************************#include <stdlib.h>
int strcmp(char *source, char *dest)
{
while(*source == *dest && *source != '\0' && *dest != '\0')
{
source++;
dest++;
}
if (*source =='\0' && *dest == '\0')
return 0;
else
return -1;
}
int main()
{
char *str1 = "abcde";
char *str2 = "abcde";
printf("ret = %d", mystrcmp(str1, str2));
return 0;
}
#include <string.h>
void quickSort(int *a, int left, int right)
{
if (left >= right)
return;
int i = left;
int j = right;
int key = a[left];
while (i < j)
{
while (i < j&&key < a[j])
j--;
a[i] = a[j];
while (i < j&&key >= a[i])
i++;
a[j] = a[i];
}
a[i] = key;
quickSort(a,left,i-1);
quickSort(a, i + 1, right);
}
int main()
{
int a[] = { 1,2,8,9,1,6,5 };
//int len=strlen(a[6]);
quickSort(a, 0, 6);
for (int i = 0;i < 7;i++)
{
printf("%d\n", a[i]);
}
system("pause");
return 0;
}
**************************************************我是分隔线*********************************************************
//单链表逆转
class node
{
public:
node* next;
int data;
}
Node * ReverseList(Node *head) //链表逆序
{
if ( head == NULL || head->next == NULL )
return head;
Node *p1 = head ;
Node *p2 = p1->next ;
Node *p3 = p2->next ;
p1->next = NULL ;
while ( p3 != NULL )
{
p2->next = p1 ;
p1 = p2 ;
p2 = p3 ;
p3 = p3->next ;
}
p2->next = p1 ;
head = p2 ;
return head ;
}
class node
{
public:
node* next;
int data;
}
Node * ReverseList(Node *head) //链表逆序
{
if ( head == NULL || head->next == NULL )
return head;
Node *p1 = head ;
Node *p2 = p1->next ;
Node *p3 = p2->next ;
p1->next = NULL ;
while ( p3 != NULL )
{
p2->next = p1 ;
p1 = p2 ;
p2 = p3 ;
p3 = p3->next ;
}
p2->next = p1 ;
head = p2 ;
return head ;
}
#include "iostream"
#include "stdio.h"
**************************************************我是分隔线*********************************************************
#include "stdio.h"
**************************************************我是分隔线*********************************************************
//函数checkstr判断一字符串是不是对称的。其中msg为输入的字符串,对称返回0,不对称返回 - 1,实现该函数。
int checkstr(const char *msg)
{
int len=strlen(msg);
int i, j;
for (i = 0,j=len-1;i<=j;i++,j--)
{
if (msg[i] != msg[j])
break;
}
if (i > j)
return 0;//对称
else
return -1;//不对称
}
void main()
{
char* msg = "olllo";
int res = 2;
res=checkstr(msg);
printf("%d\n", res);
system("pause");
}
int checkstr(const char *msg)
{
int len=strlen(msg);
int i, j;
for (i = 0,j=len-1;i<=j;i++,j--)
{
if (msg[i] != msg[j])
break;
}
if (i > j)
return 0;//对称
else
return -1;//不对称
}
void main()
{
char* msg = "olllo";
int res = 2;
res=checkstr(msg);
printf("%d\n", res);
system("pause");
}
**************************************************我是分隔线*********************************************************
1.矩阵相乘
void matMultiply(int a[x][y], int b[y][z], int c[x][z])
{
int i, j, k;
int temp;
for (i = 0;i < x;i++)
{
for (j = 0;j < z;j++)
temp = 0;
for (k = 0;k < y;k++)
{
temp+ = a[i][k] * b[k][j];
}
c[i][j] = temp;
}
}
**************************************************我是分隔线*********************************************************
二维矩阵行列交换
#define N 3
void matReverse(int mat[][N])
{
int i, j;
int temp;
for (i = 0;i < N; i++)
for (j = i+1;j < N; j++)
{
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
return;
}
void main()
{
int a[N][N] = { {1,1,1}, {2,2,2},{3,3,3} };
matReverse(a);
printf("转置后的数组为:\n");
for (i = 0;i < N;i++)
{
for (j = 0;j < N;j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
#define N 3
void matReverse(int mat[][N])
{
int i, j;
int temp;
for (i = 0;i < N; i++)
for (j = i+1;j < N; j++)
{
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
return;
}
void main()
{
int a[N][N] = { {1,1,1}, {2,2,2},{3,3,3} };
matReverse(a);
printf("转置后的数组为:\n");
for (i = 0;i < N;i++)
{
for (j = 0;j < N;j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
system("pause");
}
**************************************************我是分隔线*********************************************************