c=*(str+i); *(str+i)=*(str+j); *(str+j)=c;
时间: 2023-12-19 12:04:48 浏览: 34
These lines of code are used to swap the values of two characters in a string.
- `c` is a variable that holds the value of the character at index `i`.
- Then, the character at index `i` is replaced with the character at index `j`.
- Finally, the character originally at index `i` is replaced with the character in `c`.
This is commonly used in sorting algorithms like bubble sort or selection sort.
相关问题
帮我看看下面这段C语言程序当我输入“ABCD”是,输出的结果不是“ABCDDCBA” #include <conio.h> #include <stdio.h> #include <string.h> void fun(char *str,char *t) {int i,d; d=*str for(i=0;i<d;i++) *(t+i)=*(str+i); for(i=0;i<d;i++) *(t+d+i)=*(str+d-1-i); *(t+2*d)='\0'; } main(){ char s[100],t[100]; printf("\nPlease enter string S:"); scanf("%s",s); fun(s,t); printf("\nThe result is: %s\n",t); }
在这段程序中,变量 d 的值应该是字符串长度,但是却没有进行字符串长度的计算,而是直接将字符串第一个字符的 ASCII 码作为长度,所以当输入的字符串的第一个字符的 ASCII 码不是字符串长度时,程序就会出现错误。
例如,当输入字符串为“ABCD”时,程序会将字符 'A' 的 ASCII 码 65 作为字符串长度,因此只会复制字符串的前 65 个字符,然后程序会继续执行,将字符串的后 65 个字符再次复制到新的字符串中,这就导致了输出结果不是“ABCDDCBA”。
要解决这个问题,可以使用库函数 `strlen()` 来计算字符串的长度,将其作为参数传递给函数 `fun()`。修改后的程序如下:
```c
#include <conio.h>
#include <stdio.h>
#include <string.h>
void fun(char *str, char *t) {
int i, d;
d = strlen(str);
for (i = 0; i < d; i++)
*(t + i) = *(str + i);
for (i = 0; i < d; i++)
*(t + d + i) = *(str + d - 1 - i);
*(t + 2 * d) = '\0';
}
int main() {
char s[100], t[100];
printf("\nPlease enter string S:");
scanf("%s", s);
fun(s, t);
printf("\nThe result is: %s\n", t);
return 0;
}
```
这样,无论输入的字符串是什么,程序都能正确输出“ABCDDCBA”。
char* lower(char* str) { char *i; char *j=str; i=str; if(str==0) { return 0; } while(*i!='\0') { if(*i>='A'&&*i<='Z') { *str=*i+32; } i++; str++; } str=j; return str; }
这段代码是一个C语言函数,它的作用是将传入的字符串中的大写字母转换成小写字母,并返回转换后的字符串。具体实现方式是遍历字符串中的每个字符,如果字符是大写字母,则将其转换成小写字母。
函数的参数是一个指向字符数组的指针,即传入一个字符串。函数内部定义了两个指针变量i和j,i用于遍历字符串中的每个字符,而j则是为了在处理完字符串后返回转换后的字符串。如果传入的字符串为空指针,则直接返回0。
在while循环中,先判断指针i指向的字符是否为字符串结束符'\0',如果不是则继续遍历。如果该字符是大写字母,则将它转换成小写字母,并将结果存储在原字符串对应位置上。最后将两个指针变量都向后移动一位,以便处理下一个字符。循环结束后,将指针j重新指向字符串的开头,并返回j,即转换后的字符串。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)