编写程序,输入字符串s,将s中的字符逆序存放到字符串t中(例如,若s为“ABCDE”,则最后生成的字符串t为“EDCEA”),输出s和t的内容
实现字符串 s 中字符逆序存放到字符串 t 中的程序,它的主要流程如下:
首先,从用户处接受输入 s;
然后,利用循环将 s 中字符一个一个存入 t 中,
其次,将 t 的最后一位置为 '\0';最后,输出 s 和 t 的内容。
#include<stdio.h>
#include<string.h>
int main()
{
char s[100];
char t[100];
printf("Please input string s:\n");
scanf("%s",s);
int len=strlen(s);
for(int i=0;i<len;i++)
{
t[i]=s[len-i-1];
}
t[len]='\0';
printf("s=%s\nt=%s",s,t);
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
// Read the input string
char s[100];
printf("Enter a string: ");
scanf("%s", s);
// Calculate the length of the string
int len = strlen(s);
// Create a new string to store the reversed characters
char t[len + 1];
// Copy the characters from s to t in reverse order
for (int i = 0; i < len; i++) {
t[len - i - 1] = s[i];
}
// Add a null terminator to the end of t
t[len] = '\0';
// Print the original string and the reversed string
printf("Original string: %s\n", s);
printf("Reversed string: %s\n", t);
return 0;
}