反转字符串(c语言)

简单的反转字符串实现


#include <stdio.h>
#include "string.h"

void exchange(char *string, int c1, int c2);
void revertString(char *string, int iStart, int iEnd);

int main(int argc, const char * argv[])
{

// insert code here...
printf("Begin>>>!\n");

char originalString[100] = "abcdef";
printf("%s\n", originalString);
size_t len = strlen(originalString);
revertString(originalString, 0, (int)(len-1));
printf("%s\n", originalString);

return 0;
}

void revertString(char *string, int iStart, int iEnd)
{
while (iStart < iEnd) {
exchange(string, iStart, iEnd);
iStart++;
iEnd--;
}
}

void exchange(char *string, int c1, int c2)
{
char tmp = string[c1];
string[c1] = string[c2];
string[c2] = tmp;
}

反转字符串(c语言)

时间: 2024-08-13 21:52:43

反转字符串(c语言)的相关文章

小试牛刀之反转字符串

最近玩儿python玩上瘾了,突然想念c语言,所以,休闲下:解法一:如果没有对申请外部空间有所限制,那就先试试这个喽: 1 void invert_str1(char *old_str, char *new_str) 2 { 3 int i = strlen(old_str)-1; 4 int j = 0; 5 if (old_str == NULL) printf("error!"), exit(1); 6 bzero(new_str, sizeof(new_str)); 7 for

golang——reverse反转字符串

reverse反转,是个比较基础算法.要实现这个方法,从常理考虑可以申请一个新空间,然后将字符串的从尾到头依次填充该空间,最后新空间的内容就是反转后的结果了,这个方式的算法复杂度是O(n),并且还需要重新申请空间. 然而通过对字符串前后对调实现的,方法非常优雅,复杂度一下就降到了O(n/2).用golang语言模拟如下: package main import ( "fmt" ) func main() { s := "hello,golang语言" fmt.Pri

反转字符串

问题: 1.反转字符串,比如str=“hello world!!!",反转后ret=“!!!dlrow olleh"; 代码如下: #include <stdio.h> #include <stdlib.h> char* reverse(char inp[],int size){ if(size<0) return NULL; //NULL代表空地址,null只是一个符号 int i=0,j=size-1; while(i<j){ char tmp=

[算法] C# Revert 单词反转字符串[低时间复杂度]

无聊期间想起了一道字符串反转的问题. 大致要求输入"I am a good boy",输出"boy good a am I". 要求不能用已经封装好的方法实现.于是乎,我上网查了一下,基本都是用了封装后类库.于是我自己写了一个小算法,低时间复杂度高空间复杂度的算法. private string Revert(string str) { if (str.Length == 0) { return string.Empty; } string newStr = nul

345. 反转字符串中元音字母的位置 Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede" 题意:反转字符串中元音字母的位置 方法1:用栈保存元音字符串,时间

C# 反转字符串方法

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 反转字符串 { class Program { static void Main(string[] args) { string ss = Reserver("abcdefg"); Console.Write(ss); //数组里面有一个方法用来反转的 除了今天上午用到这个还可以用这个 } ///

Java反转字符串

前几天看见一篇文章,说使用Java能用几种方式反转一个字符串.首先要明白什么叫反转字符串,就是将一个字符串到过来啦,比如"倒过来念的是小狗"反转过来就是"狗小是的念来过倒".接下来就把自己能想到的所有方式记录下来了. 1.第一个念头就是直接使用String类的反转方法,对不起,这样是不行的,因为String类没有这个方法.那么好吧,搞个数组,然后遍历数组,依次调换数组中对应的各个字符. // 直接使用数组首位调换 public String reverse1(Str

【LeetCode-面试算法经典-Java实现】【152-Reverse Words in a String(反转字符串中的单词)】

[152-Reverse Words in a String(反转字符串中的单词)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". 题目大意 给定一个字符串,将其反转,其的字词不转 解题思路

使用递归算法反转字符串

public class T { //反转字符串 public static String reverseString(String s){ if(s.isEmpty()) return s; return reverseString(s.substring(1))+s.charAt(0); } public static void main(String[] args) { System.out.println(reverseString("123456789")); } }