当前位置:首页 » 编程语言 » c语言字符串数据交换
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言字符串数据交换

发布时间: 2022-11-16 15:23:40

c语言 把某一字符串中的其中一段字符串替换成另一串字符串。

#include
<stdio.h>
#include
<string.h>
void
main(void)
{
char
getstr[100];
char
sendstr[100];
char
a_b[100];
char
i;
printf("please
input
the
firs
str!\n");
gets(getstr);
printf("please
input
the
second
str
!\n");
gets(sendstr);
strcpy(a_b,getstr);
//交换两个字符串的数据
strcpy(getstr,sendstr);
strcpy(sendstr,a_b);
for(i
=
0;
getstr
!=
'\0';
i++)
{
printf("str1:%c",getstr[i]);
}
for(i
=
0;
getstr
!=
'\0';
i++)
{
printf("str2:%c",sendstr[i]);
}
}
这是将两个字符串进行交换后输出。
第二个问题则用到了数据结构了,使用一个结构体链表,使用strcmp()或strcmpi()比对你输入的字符串,找到相同的输出对应的中文字符串就好了。
其中:
strcmp()
对两个字符串进行大小写敏感的比较;strcmpi()
对两个字符串进行大小写不敏感的比较;

Ⅱ 在c语言二维数组中怎样交换两个数组元素中的字符串,请写代码.

int strcpy(char *s1,const char *s2);
开辟一个缓冲区,比如
char buff[100];//假设你的字符串不超过这么多

而你的而为数组为
char **argv;
其中argv[0] = "this is the first string";
argv[1] = "this is the seconde string";

你只须调用如下
strcpy(buff,argv[0]);
strcpy(argv[0],argv[1]);
strcpy(argv[1],buff);

一下是完整代码,并测试过
#include <stdio.h>
#include <string.h>

char argv[2][100]={ "This is the first string","This is the second string"};
// 存储字符串的二维数组,每个字符串最长为99个字节
char buff[100];
//缓冲区
int main()
{
printf("转换前:\n");
printf("argv[0] = %s\n",argv[0]);
printf("argv[1] = %s\n",argv[1]);

strcpy(buff,argv[0]);
strcpy(argv[0],argv[1]);
strcpy(argv[1],buff);

printf("转换后:\n");
printf("argv[0] = %s\n",argv[0]);
printf("argv[1] = %s\n",argv[1]);

return 0;
}

Ⅲ c语言中如何实现任意数据类型变量值得交换

#include<stdio.h>
#define SCANF(m) scanf("%"#m"%"#m,&m##a,&m##b)
#define PRINTF(m) printf("交换结果:%"#m"\t%"#m"\n",m##a,m##b)
void swap(char,void*,void*,void*);
int main()
{
unsigned ua,ub,ut;
int da,db,dt;
float fa,fb,ft;
double lfa,lfb,lft;
char ca,cb,ct,ch;
printf("数据类型:");
printf("u-无符号整型\ti-有符号整型\tf-单精度实型\td-双精度实型\tc-字符型\n");
printf("请选择\n");
scanf("%c",&ch);
fflush(stdin);
printf("请输入两个指定类型的数据:");
switch(ch)
{
case 'u':SCANF(u);swap(ch,&ua,&ub,&ut);PRINTF(u);break;
case 'i':SCANF(d);swap(ch,&da,&db,&dt);PRINTF(d);break;
case 'f':SCANF(f);swap(ch,&fa,&fb,&ft);PRINTF(f);break;
case 'd':SCANF(lf);swap(ch,&lfa,&lfb,&lft);PRINTF(lf);break;
case 'c':SCANF(c);swap(ch,&ca,&cb,&ct);PRINTF(c);break;
}
}
void swap(char c,void *p1,void *p2,void *p3)
{
switch(c){
case 'u':*(unsigned *)p3=*(unsigned *)p1;*(unsigned *)p1=*(unsigned *)p2;*(unsigned *)p2=*(unsigned *)p3;break;
case 'i':*(int *)p3=*(int *)p1;*(int *)p1=*(int *)p2;*(int *)p2=*(int *)p3;break;
case 'f':*(float *)p3=*(float *)p1;*(float *)p1=*(float *)p2;*(float *)p2=*(float *)p3;break;
case 'd':*(double *)p3=*(double *)p1;*(double *)p1=*(double *)p2;*(double *)p2=*(double *)p3;break;
default :*(char *)p3=*(char *)p1;*(char *)p1=*(char *)p2;*(char *)p2=*(char *)p3;break;
}
} 这个是我帮我一个兄弟做的,你看行不?

Ⅳ C语言程序题:数据交换,从键盘输入a、b的值,输出交换以后的值

//思路一:采用第三者中间变量
main()
{inta,b,t;
scanf("%d%d",&a,&b);
printf("交换前:a=%d,b=%d ",a,b);
t=a;
a=b;
b=t;
printf("交换后:a=%d,b=%d ",a,b);
}

//思路二:利用数学计算方法
main()
{inta,b;
scanf("%d%d",&a,&b);
printf("交换前:a=%d,b=%d ",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("交换后:a=%d,b=%d ",a,b);
}

Ⅳ 定义两个变量ab如何用c语言编程使两个变量的值能相互交换

1、首先,就是最基本的知识,写头文件,函数声明,定义变量。

Ⅵ c语言 连续 数据 前后 交换,比如12 34 56 变成21 43 65

#include"stdio.h"
char* swap_str(char str[])
{
char c;
for(int i= 0; str[i]!='\0'; i++)
{
if(str[i]!=' ')
{
c= str[i];
str[i]= str[i+1];
str[i+1]= c;
i++;
}
}
return str;
}
void main()
{
char s[100];
puts("请输入字符串:");
gets(s);
printf("%s\n", swap_str(s));
}

Ⅶ c语言怎么实现两个数据交换

将两个数进行互换的方法有两种形式:
1、借助中间变量完成,此方法直观,易理解,使用最多
2、不需要中间变量,通过变量身的运算完成交换。

Ⅷ C语言 如何交换两个字符型数据值

在C语言char
就是
unsigned
int
类型。所以可以直接按照,整型的交换方法来进行。
例如下例:
输出为
如果是字符串,可以使用strcpy函数。
例如下例
输出为:
请采纳

Ⅸ C语言数值的交换

楼上的这个
a = a+b;
b = a-b;
a = a-b;
是个方法,是符合我们数学思维的方法,也是最初接触C语言的人可能想到的方法。 但是这样编程很不直观,不如t=a;a=b;b=t;来得快。

似乎在C++中有swap(a, b)模板函数,直接实现a,b交换。

想玩高级一点的话,可采用“换标不换值”的方法,用数组元素作为数组的下标,这种方法换逻辑不换存储。

#include <stdio.h>
void main()
{
int a=10,b=20;
int array1[2] = {10,20}; //存a、b值
int array2[2] = {0,1}; //存下标
b = array1[array2[0]];
a = array1[array2[1]];
printf("a=%d, b=%d\n",a,b);
}

这个方法在对结构体数组值交换中非常好用!因为结构体数组一般每个成员都有很多个值,如:
struct student
{
int num;
double score;
char name[20];
}stu[5]={{1,98,"ziguowen"},{2,88,"dongda"},{3,78,"haha"}};

//交换stu[0]和stu[1],需要
int n;
double s;
char n[20];
n = stu[0].num; stu[0].num = stu[1].num; stu[1].num = n;
s = stu[0].score; stu[0].score = stu[1].score; stu[1].score = s;
strcpy(n, stu[0].name); strcpy(stu[0].name, stu[1].name); strcpy(stu[0].name,s);

//而用下标的话,一个赋值语句即可,直接交换stu[0] 和 stu[1] 下标后全部交换!

Ⅹ C语言字符串逆转函数

  1. 只需要将字符数组的前后数据进行交换就行了。


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

    char*reverse(char*x)
    {
    inti,tem,len=strlen(x);
    intn=len/2;
    for(i=0;i<=n;i++)//前后交换数据
    {
    tem=x[i];
    x[i]=x[len-1-i];
    x[len-1-i]=tem;
    }
    returnx;
    }

    intmain()
    {
    charp[]="abcdefghijk";
    printf("relust=%s ",reverse(p));
    return0;
    }