#include<stdio.h> int main(){ int i=0,k=0,s; char ch[80]; while((ch[k]=getchar())!='\n') k++; ch[k]='\n'; s=k; for(k=0;k<s/2;k++){ if(ch[k]!=ch[s-1-k]){ for(int i=0;i<s;i++) printf("%c",ch[i]); printf("\n"); printf("No"); } else(ch[k]==ch[s-1-k]);{ for(i=0;i<s;i++) printf("%c",ch[i]); printf("\n"); printf("Yes"); } }return 0; }
时间: 2024-03-29 10:41:34 浏览: 43
这段代码是一个判断回文字符串的程序。用户输入一行字符串,程序会判断该字符串是否是回文字符串(即正向和反向拼写都一样)。如果是回文字符串,则输出 "Yes",否则输出 "No"。
程序中使用了一个字符数组 ch 来存储用户输入的字符串,使用变量 k 来记录输入的字符串的长度。程序中通过循环遍历 ch 数组,判断字符串是否是回文字符串。如果不是回文字符串,则直接输出 "No" 并退出程序;如果是回文字符串,则直接输出 "Yes" 并退出程序。
需要注意的是,程序中有一个小错误,即在第 16 行的 else 后面多了一对花括号,应该将它删除。
相关问题
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;
}
```
Nirvana 15:38:49 #include <stdio.h> #include <conio.h> #include <stdbool.h> #define MAX_QUEUE_SIZE 1000 void main() { char queue[MAX_QUEUE_SIZE]; int front=0,rear=0; char ch; ch=getchar(); while(ch!='#') { queue[rear]=ch; rear++; ch=getchar(); } while(front<rear) { printf("%c",queue[front]); front++; } printf("\n"); getch(); } Nirvana 15:39:07 #include <stdio.h>#include <conio.h>#define MAX_STACK_MAX 1000void main(){ char stack[MAX_STACK_MAX]; int top=0,i; char ch;ch=getchar(); while (ch!='!'&&top<MAX_STACK_MAX) { switch(ch) { case'#': if(top>0) top--; break; case'@': top=0; break; default: stack[top]=ch; top++; break; } ch=getchar(); } for(i=0;i<top;i++) printf("%c",stack[i]); printf("\n"); getch();}将这两段代码合为一段代码
以下是将两段代码合并后的代码:
```c
#include <stdio.h>
#include <conio.h>
#include <stdbool.h>
#define MAX_QUEUE_SIZE 1000
#define MAX_STACK_MAX 1000
void main() {
char queue[MAX_QUEUE_SIZE];
int front = 0, rear = 0;
char stack[MAX_STACK_MAX];
int top = 0, i;
char ch;
ch = getchar();
while (ch != '#') {
queue[rear] = ch;
rear++;
ch = getchar();
}
ch = getchar();
while (ch != '!' && top < MAX_STACK_MAX) {
switch(ch) {
case '#':
if (top > 0) {
top--;
}
break;
case '@':
top = 0;
break;
default:
stack[top] = ch;
top++;
break;
}
ch = getchar();
}
printf("Queue output: ");
while (front < rear) {
printf("%c", queue[front]);
front++;
}
printf("\n");
printf("Stack output: ");
for (i = 0; i < top; i++) {
printf("%c", stack[i]);
}
printf("\n");
getch();
}
```
这个合并后的代码可以分别读入字符到队列和栈中,并输出它们的内容。其中,遇到 `#` 时队列弹出元素,遇到 `@` 时栈清空。
阅读全文