c操作翻转字符串
#include<stdio.h>
#include<malloc.h>
#include<string.h>
/*
* 翻转
*/
char *mystrrev(char *arr)
{
if (!arr)
{
return NULL;
}
char *temp = arr;
char t;
int leng = strlen(arr) + 1;
int l = (int)(leng / 2);
int i = 0;
while (l--)
{
t = arr[i];
arr[i] = arr[leng - 2];
arr[leng - 2] = t;
i++;
leng--;
}
return temp;
}
/*
*截取单个词汇
*/
char *myrev(char *string)
{
if (!string)
{
return NULL;
}
char *temp1 = (char *)malloc(strlen(string) + 1);
char *temp2 = (char *)malloc(strlen(string) + 1);
char *temp3 = NULL;
*temp2 = '\0';
int i = 0;
for (int j=strlen(string); j>=0; --j)
{
temp1[i] = *string;
if (temp1[i] == ' ' || *(string) == '\0')
{
if (!temp3)
{
free(temp3);
temp3 = NULL;
}
temp3 = (char *)malloc(i);
temp1[i] = '\0';
i = 0;
strcpy(temp3, temp1);
strcat(temp2, mystrrev(temp3));
strcat(temp2, " ");
i = -1;
}
i++;
string++;
}
if (!temp1)
{
free(temp1);
temp1 = NULL;
}
if (!temp3)
{
free(temp3);
temp3 = NULL;
}
return temp2;
}
int main(int argc, char *argv[])
{
char *src = "Today is a good day";
char *s = "abcd_*__abcd";
char *temp = myrev(src);
char *temp1 = myrev(s);
printf("%s\n", temp);
printf("%s\n", temp1);
return 0;
}
结果:
yadoT si a doog yad
dcba__*_dcba
相关文档:
拿到这本电子书看了林博士写的前言,讲述的什么是编程老手与编程高手,此时我才知我只能称得上是业余编程爱好者而已,林博士对编程老手与编程高手做了如下的定义:
定义 1:能长期稳定地编写出高质量程序的程序员称为编程老手。
定义 2:能长期稳定地编写出高难度、高质量程序的程序员称为编程高手。 ......
最近对基础知识进行了学习,发现以前很多东西都没有搞清楚
1. 编译的问题,头文件主要是定义
//////// add.c
int add(int a, int b)
{
return a + b;
}
///////// main.c
#include <stdio.h>
int add(int a, int b);
int main ()
{
printf("%d" ......
标签:
it
分类:C/C++
我的回忆和有趣的故事 --- C/C++圣战篇
李维
------------------------------------------------------------------------------------------
声明
以下的这篇文章内容是我个人的回忆以及看法,没有任何特别的偏见,许多的事情是根据我的记忆以及从许多人的诉说中得知的,也许内容不是百分 ......
ODBC和IDAPI之争
当Microsoft在逐渐的击败他的竞争对手,并且拥有了大部份PC数据库市场之后,便慢慢的了解到掌握标准的重要性。此外Microsoft为了统一各应用程序之间不同资料的存取,因此开始制定存取资料的统一标准-ODBC。Microsoft更大的目的是为了准备和瞄准下一场的大战,那就是PC上的RDBMS产品。当然Microsoft 要一统 ......
C/C++ 常见误区
1. C++虽然主要是以C的基础发展起来的一门新语言,但她不是C的替代品,不是C的升级,C++和C是兄弟关系。没有谁比谁先进的说法,更重要的一点是C和C++各自的标准委员会是独立的,最新的C++标准是C++98,最新的C标准是C99。因此也没有先学C再说C++的说法,也不再(注意这个"不再")有C++语法 ......