#include <stdio.h> fun (char *s, int n1, int n2) char c: while (n1<n2) c= sinll:sinll=sIn2l:sIn2]=c: n1++; n2-- D、 main) char all="ABCD": fun (a, 0,3) ; printf("gs\n",a):
时间: 2024-02-19 16:57:52 浏览: 69
这段代码有语法错误,应该是:
```
#include <stdio.h>
void fun(char *s, int n1, int n2) {
char c;
while (n1 < n2) {
c = s[n1];
s[n1] = s[n2];
s[n2] = c;
n1++;
n2--;
}
}
int main() {
char a[] = "ABCD";
fun(a, 0, 3);
printf("%s\n", a);
return 0;
}
```
这段代码的作用是将字符串 "ABCD" 中下标在 n1 至 n2 之间的字符反转。在 main 函数中,我们首先将字符串 "ABCD" 存储在字符数组 a 中,然后调用函数 fun,将其反转。最后,我们打印反转后的字符串 "DCBA"。
相关问题
6-4 字符数组与字符串:求带符号的数字串对应的整数。 分数 10 作者 苑丽红 单位 长春理工大学 请编写一个函数fun,它的功能是:将一个数字字符串转换为一个整数(不得调用C语言提供的将字符串转换为整数的函数)。例如,若输入字符串"-1234",则函数把它转换为整数值 -1234。 (请勿在答题框编写主函数main, 仅需完成函数fun的begin和end之间应该填写 的代码。) 函数接口定义: long fun ( char *p); 其中 p指向主函数传入的带符号的数字字符串。 裁判测试程序样例: #include <stdio.h> #include <string.h> long fun ( char *p) ; int main() /* 主函数 */ { char s[6]; long n; scanf("%s", s) ; n=fun(s); printf("%ld",n); return 0; } /* 答题框所填写的代码将被嵌入到这里 */ 输入样例: -4567 输出样例: -4567 输入样例: 456 输出样例: 456
#include <stdio.h>
#include <string.h>
long fun(char *p) {
int len = strlen(p);
int sign = 1; // 符号位,默认为正数
int i = 0;
long result = 0;
// 判断是否有符号位
if (p[0] == '-') {
sign = -1;
i = 1;
}
// 遍历字符串并转换为整数
for (; i < len; i++) {
if (p[i] >= '0' && p[i] <= '9') {
result = result * 10 + (p[i] - '0');
}
}
return result * sign;
}
1.创建文件夹: #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <iostream> using namespace std; int main() { string folder_name = "new_folder"; mkdir(folder_name.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); //创建文件夹 return 0; } 2.复制文件: #include <stdio.h> #include <stdlib.h> int main() { FILE *fp1, *fp2; //定义两个文件指针 char ch; fp1 = fopen("file1.txt", "r"); //打开要复制的文件 fp2 = fopen("file2.txt", "w"); //打开要复制到的文件 while ((ch = fgetc(fp1)) != EOF) { fputc(ch, fp2); //复制文件 } fclose(fp1); fclose(fp2); return 0; } 3.移动文件: #include <stdio.h> #include <stdlib.h> int main() { char old_path[100] = "old_folder/file1.txt"; char new_path[100] = "new_folder/file1.txt"; int result = rename(old_path, new_path); //移动文件 if (result == 0) { printf("移动成功\n"); } else { printf("移动失败\n"); } return 0; } 4.删除文件夹: #include <unistd.h> #include <stdio.h> int main() { char folder_name[100] = "new_folder"; int result = rmdir(folder_name); //删除文件夹 if (result == 0) { printf("删除成功\n"); } else { printf("删除失败\n"); } return 0; } 5.显示文件夹中的内容: #include <dirent.h> #include <stdio.h> int main() { DIR *dir; struct dirent *ent; char folder_name[100] = "new_folder"; dir = opendir(folder_name); //打开文件夹 while ((ent = readdir(dir)) != NULL) { printf("%s\n", ent->d_name); //遍历文件夹中的文件 } closedir(dir); return 0; } 6.查看文件内容: #include <stdio.h> int main() { FILE *fp; char ch; fp = fopen("file1.txt", "r"); //打开文件 while ((ch = fgetc(fp)) != EOF) { printf("%c", ch); //输出文件内容 } fclose(fp); return 0; } 7.修改文件权限: #include <sys/stat.h> #include <stdio.h> int main() { char file_name[100] = "file1.txt"; chmod(file_name, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); //修改文件权限 return 0; } 8.搜索文件: #include <dirent.h> #include <stdio.h> #include <string.h> int main() { DIR *dir; struct dirent *ent; char folder_name[100] = "new_folder"; char search_name[100] = "file1.txt"; dir = opendir(folder_name); //打开文件夹 while ((ent = readdir(dir)) != NULL) { if (strcmp(ent->d_name, search_name) == 0) //搜索文件 { printf("找到文件:%s\n", ent->d_name); break; } } closedir(dir); return 0; }将上述代码整合成一个完整的程序代码
以下是整合后的完整代码:
```
#include <iostream>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
int createFolder(string folder_name) {
int result = mkdir(folder_name.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); //创建文件夹
if (result == 0) {
cout << "创建文件夹成功" << endl;
} else {
cout << "创建文件夹失败" << endl;
}
return result;
}
int copyFile(char* old_path, char* new_path) {
FILE *fp1, *fp2; //定义两个文件指针
char ch;
fp1 = fopen(old_path, "r"); //打开要复制的文件
fp2 = fopen(new_path, "w"); //打开要复制到的文件
if (fp1 == NULL || fp2 == NULL) {
cout << "打开文件失败" << endl;
return -1;
}
while ((ch = fgetc(fp1)) != EOF) {
fputc(ch, fp2); //复制文件
}
fclose(fp1);
fclose(fp2);
return 0;
}
int moveFile(char* old_path, char* new_path) {
int result = rename(old_path, new_path); //移动文件
if (result == 0) {
cout << "移动文件成功" << endl;
} else {
cout << "移动文件失败" << endl;
}
return result;
}
int deleteFolder(char* folder_name) {
int result = rmdir(folder_name); //删除文件夹
if (result == 0) {
cout << "删除文件夹成功" << endl;
} else {
cout << "删除文件夹失败" << endl;
}
return result;
}
int displayFolder(char* folder_name) {
DIR *dir;
struct dirent *ent;
dir = opendir(folder_name); //打开文件夹
while ((ent = readdir(dir)) != NULL) {
cout << ent->d_name << endl; //遍历文件夹中的文件
}
closedir(dir);
return 0;
}
int displayFile(char* file_name) {
FILE *fp;
char ch;
fp = fopen(file_name, "r"); //打开文件
if (fp == NULL) {
cout << "打开文件失败" << endl;
return -1;
}
while ((ch = fgetc(fp)) != EOF) {
cout << ch; //输出文件内容
}
fclose(fp);
return 0;
}
int changePermission(char* file_name) {
int result = chmod(file_name, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); //修改文件权限
if (result == 0) {
cout << "修改文件权限成功" << endl;
} else {
cout << "修改文件权限失败" << endl;
}
return result;
}
int searchFile(char* folder_name, char* search_name) {
DIR *dir;
struct dirent *ent;
dir = opendir(folder_name); //打开文件夹
while ((ent = readdir(dir)) != NULL) {
if (strcmp(ent->d_name, search_name) == 0) { //搜索文件
cout << "找到文件:" << ent->d_name << endl;
closedir(dir);
return 0;
}
}
cout << "未找到文件" << endl;
closedir(dir);
return -1;
}
int main() {
string folder_name = "new_folder";
char old_path[100] = "old_folder/file1.txt";
char new_path[100] = "new_folder/file1.txt";
char file_name[100] = "file1.txt";
char search_name[100] = "file1.txt";
createFolder(folder_name);
copyFile(old_path, new_path);
moveFile(old_path, new_path);
deleteFolder("new_folder");
displayFolder("new_folder");
displayFile(file_name);
changePermission(file_name);
searchFile("new_folder", search_name);
return 0;
}
```
阅读全文