字符串逆转

头文件:

1 #include<stdlib.h>
2 #include<stdio.h>
3 #include<string.h>

函数原型:

void inverse(char *str);

方法一:

void inverse(char *str){
    char *p1, *p2;

    char temp;

    p1 = str;
    p2 = str + strlen(str) - 1; 

    while(p1 < p2){
        temp = *p1;
        *p1 = *p2;
        *p2 = temp;
        p1++;
        p2--;
    }

}

方法二:

 1 void inverse(char *str){
 2     int i, j, temp;
 3
 4     i = 0;
 5     j = strlen(str) - 1;
 6
 7     while (i < j){
 8         temp = str[i];
 9         str[i] = str[j];
10         str[j] = temp;
11         i++;
12         --j;
13     }
14
15 }
时间: 2024-08-08 14:18:28

字符串逆转的相关文章

字符串逆转-压缩连续空格

一.算法描述 给定一个字符串,对于字符串包括多个连续空格的情况,压缩并只保留一个空格,同时以空格隔离的子串逆转. 二.算法思路 其本质是字符串逆转的变体,即在字符串逆转的基础上,还要压缩多余空格,这种情况相比单纯的字符串逆转多了一步判断连续空格的条件 如下代码中,包括单纯的字符串逆转函数,实现比较简单,主要是注意下标的位置 三.算法代码 #include <iostream> #include <string.h> #include <stdio.h> #include

将字符串逆转

1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<ctype.h> 5 6 void reverse(char *str); 7 8 int main() 9 { 10 char str[128]; 11 while(gets_s(str,128) != NULL) 12 { 13 //deleteSpace(str); 14 reverse(str); 15 p

HDU 1266 Reverse Number(字符串逆转 水题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1266 Problem Description Welcome to 2006'4 computer college programming contest! Specially, I give my best regards to all freshmen! You are the future of HDU ACM! And now, I must tell you that ACM proble

关于js 字符串逆转

有个同学问我一道题目 当时在车上,电脑没在旁边,看到这个问题我就想好了答案,递归和for循环实现早就在脑海里code好了,之后回到家写好代码 //递归 function fun1(str,i){ i = i||0; var j = str.length - (i+1); if(j<=i){ return true; } if(str[i] != str[j]){ return false; } return arguments.callee(str,i+1); } //for function

字符串逆转的七种方式

1.列表的方式 def func_01(s): a = list(s) a.reverse() a = "".join(a) return a b = func_01("hello") print(b) 2.切片的方式 def func_02(s): s = s[::-1] return s c = func_02("world") print(c) 3.reduce from functools import reduce def func_0

带空格的字符串逆转(简洁版)

#include<iostream>#include<algorithm>#include<string>using namespace std; int main(){    string str;    getline(cin,str);    reverse(str.begin(),str.end());    cout<<str;    return 0;} 原文地址:https://www.cnblogs.com/zhoumin6012/p/104

自己用c语言实现字符串处理库函数以及扩展

1.实现基本的c语言库函数: int myStrlen( const char* str);//根据传入的字符串首地址获取字符串长度:返回值为长度 int myStrlen(const char* str) { if (str == NULL) return 0; int length=0; while (*str != '\0') { length++; str++; } return length; } void* myStrcpy(char* des, const char* source

微软之左旋转字符串

时间:2014.04.29 地点:基地二楼 ---------------------------------------------------------------------------------------------- 一.题目 定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部. 如把字符串abcdef左旋转2位得到字符串cdefab. ------------------------------------------------------------- 二.

KMP字符串匹配算法及next前缀数组的应用

#KMP字符串匹配算法及next前缀数组的应用------ KMP算法通常是我们学习字符串匹配算法时遇见的第一个算法,另外还有Rabin-Karp, Sunday算法等. 相对于其他字符串匹配算法, kmp在字符串中字符重复率低的情况下并不具备优势,那为什么KMP算法会作为经典的教学算法呢? 原因可能是:KMP算法充分利用next前缀数组的信息来优化算法,减小时间复杂度的思路在很多字符串相关问题中能给我们启发. 首先上KMP字符串匹配算法, [leetcode在线测试地址](https://le