实现字符串数组中字符串交换

实现字符串数组中字符串交换

2015-06-01  青岛  张俊浩

《实现字符串数组字符串交换》分为三部分:

【1】论坛帖子

【2】代码思路

【3】代码、运行结果

1.论坛帖子

2.代码思路

(1)两个字符串等长正常交换即可;

(2)两个字符串不等长交换短字符串宽度的字符数据(包括结束符),拷贝长字符剩余字符到短字符串数据区(此时长字符串剩余数据还在内存只是被结束符’\0’分割)。

3.代码、运行结果

#include<stdio.h>
void swap(char *a,char *b)
{
	char temp = -1;

	do{
       temp = *a;
       *a = *b;
	   *b = temp;

	   ++a;
	   ++b;
	}while(*a != '\0' && *b != '\0');//遇到两者之一结束时,终止交换

	if(*a =='\0'){//此时结束符未交换位置,说明字符串a先结束,只把字符串b剩余部分拷贝到字符串a位置就好
	   temp = *a;
       *a = *b;
	   *b = temp;//交换结束符

	   ++a;
	   ++b;

		do{
			*a++ = *b++;
		}while(*b != '\0');//拷贝b剩余字符(结束符未拷贝)
		*a = *b;//拷贝结束符
	}
	else{//此时结束符未交换位置,说明字符串b先结束,只把字符串a剩余部分拷贝到字符串b位置就好
	   temp = *b;
       *b = *a;
	   *a = temp;//交换结束符

		do{
			*b++ = *a++;
		}while(*a != '\0');//拷贝a剩余字符(结束符未拷贝)
		*b = *a;//交换结束符
	}

}
void main()
{
  char a[][7]={"foo","bar","foobar"};
 	printf("old:%-6s\t%-6s\t%-6s\n",a[0],a[1],a[2]);
    swap(a[0],a[2]);
	printf("new:%-6s\t%-6s\t%-6s\n",a[0],a[1],a[2]);
}

时间: 2024-08-29 03:00:09

实现字符串数组中字符串交换的相关文章

字符串数组 输入3个字符串,要求按由小到大的字母顺序输出; 输入n个学生的姓名和学号到字符串数组中,在输入一个姓名,如果班级有该生则返回其信息,否则返回本班无此人

输入3个字符串,要求按由小到大的字母顺序输出 如 输入franch england china,输出结果是china england franch 三个数排序输出,比较三个数的大小怎么做? a=18 b= 9 c=30 a>b 交换 a=9 b=18 可不可以用选择法?可不可以用冒泡法?但是这里用不着 例题: string1 string2 string3 temp a=18 b= 9 c=30 b>c? 交换 b中放的是b 和 c 中小的数,c中放的是bc中大的数. 如果a<b,则输出

07.C#中如何排除/过滤/清空/删除掉字符串数组中的空字符串

方式一:使用lambda表达式筛选过滤掉数组中空字符串         1 /// <summary> 2 /// 使用lambda表达式排除/过滤/清空/删除掉字符串数组中的空字符串 3 /// </summary> 4 /// <param name="args"></param> 5 static void Main(string[] args) 6 { 7 string[] strArray = { "",&q

字符串数组中两个字符的最短距离

[leetcode] https://leetcode.com/problems/shortest-word-distance/ For example,Assume that words = ["practice", "makes", "perfect", "coding", "makes"]. Given word1 = "coding", word2 = "practic

Substring with Concatenation of All Words, 返回字符串中包含字符串数组所有字符串元素连接而成的字串的位置

问题描述:给定一个字符数组words,和字符串s,返回字符数组中所有字符元素组成的子串在字符串中的位置,要求所有的字符串数组里的元素只在字符串s中存在一次. 算法分析:这道题和strStr很类似.只不过strStr是子串,而这个题是字符串数组里的元素组成的子串,字符串数组里的元素是无序的,但是必须全部包含.所有考虑使用map集合.关键点在于几个临界值,字符串元素在s中重复了怎么做,找到一个符合的子串后怎么做,有字符串元素不匹配怎做. import java.util.ArrayList; imp

[码海拾贝 之Perl]在字符串数组中查找特定的字符串是否存在

前言 检索一个字符串是否存在于一个数组中, 最基本的想法应该就是对数组进行循环, 逐个判断数组的每个元素值和给定的值是否相等. (在Java语言还可以把数组转成 List , 在 list 中直接有 contains 方法可以使用) 看一段简单的代码: my @arr2 = qw(str1 str2 str3 str4); foreach(@arr2) { if($_ eq "str2") { print "str2 exist in Array!\n"; last

LeetCode -- 求字符串数组中的最长公共前缀

题目描写叙述: Write a function to find the longest common prefix string amongst an array of strings.就是给定1个字符串数组,找出公共最长前缀. 思路非常直接.使用1个索引来存最长公共前缀的长度就能够了. 注意, 假设使用1个字符串变量来存前缀的话,是不能AC的,由于题目不同意使用额外的空间. public string LongestCommonPrefix(string[] strs) { if(strs

字符串数组和字符串的转换

1.StringUtils.join(Object array[],String separator) 将数组以符号或其他字符串为间隔组成新的字符串 Object array[] 需要转换的数组.separator组成新串的间隔符号,如 "," "|" private static final String[] str = {"1","2","3","4"}; 2 String str2

字符串数组与字符串之间的互转(join/split)

1.Java 1-1.字符串数组=>字符串:StringUtils: join(Object[] array, String separator) 例: Java代码 收藏代码 import org.apache.commons.lang.StringUtils; public class StringUtilsTrial { public static void main(String[] args) { // Join all Strings in the Array into a Sing

查找字符串数组中的最长公共前缀

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" con