K&;R C编程练习1-19
任务: 编写一个反转函数,反转字符串。使用它编写一个程序,每次反转一行输入 以下是我试图解决的问题:K&;R C编程练习1-19,c,string,char,reverse,kernighan-and-ritchie,C,String,Char,Reverse,Kernighan And Ritchie,任务: 编写一个反转函数,反转字符串。使用它编写一个程序,每次反转一行输入 以下是我试图解决的问题: #include <stdio.h> #define MAXLINE 1000 int getlinex(char line[], int maxline); int main(){ int len, j; char line[MAXLINE]; char *temp; while ((len = getlinex(line, MAXLINE))
#include <stdio.h>
#define MAXLINE 1000
int getlinex(char line[], int maxline);
int main(){
int len, j;
char line[MAXLINE];
char *temp;
while ((len = getlinex(line, MAXLINE)) > 0) {
for(j=0; j<=len; j++){
temp = &line[len-j];
line[j] = *temp ;
}
printf("%s", line);
}
}
int getlinex(char s[], int lim) {
int c, i;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++) {
s[i] = c;
}
if (c == '\n') {
s[i] = c;
i++;
}
s[i] = '\0';
return i;
}
#包括
#定义MAXLINE 1000
int getlinex(字符行[],int maxline);
int main(){
int len,j;
字符行[MAXLINE];
字符*温度;
而((len=getlinex(line,MAXLINE))>0){
对于(j=0;j 0){
对于(j=0;j,当它写在赋值中时,你必须写一个相应的函数。它可以按照下面的方式显示,如演示程序所示
#include <string.h>
#include <stdio.h>
char * reverse( char *s )
{
size_t n = strlen( s );
for ( size_t i = 0; i < n / 2; i++ )
{
char c = s[n-i-1];
s[n-i-1] = s[i];
s[i] = c;
}
return s;
}
int main( void )
{
char s[] = "Hello, World!";
puts( s );
puts( reverse( s ) );
}
至于程序中的循环
for(j=0;j<=len;j++){
temp = &line[len-j];
line[j] = *temp ;
}
也许你应该在反转它之前把它从字符串中移除
比如说
if ( line[len-1] == '\n' ) line[len-1] = '\0';
或者只需从函数getlinex
中删除上面显示的if
语句就可以了。请考虑以下循环:
for(j=0;j<=len;j++){
temp = &line[len-j];
line[j] = *temp;
}
(j=0;j下面是我在进行KnR练习时编写的代码
#include <stdio.h>
/* exercise 1.19 */
#define MAXLEN 1000
int getline(char s[], int len);
void reverse(char s[]);
int
main(){
int len;
char line[MAXLEN];
while ((len=getline(line,MAXLEN))){
reverse(line);
printf("%d - %s",len, line);
}
return 0;
}
/*
* Reads a line from stdin
*/
int getline(char s[], int len){
int c, i;
for (i = 0; i < len -1 && (c=getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if (c == '\n'){
s[i] = '\n';
++i;
}
s[i]='\0';
return i;
}
/*
* Reverses the order of chracters in string s
*/
void reverse(char s[]){
int i, k;
char tmp[MAXLEN];
i = 0;
/* copy the character string */
while ((tmp[i] = s[i]) != '\0')
++i;
--i; /* last character is nil */
for (k = 0; k <= i ; ++k)
s[k] = tmp[i-k];
s[k] = '\0';
}
#包括
/*练习1.19*/
#定义MAXLEN 1000
int getline(字符s[],int len);
无效反向(字符s[]);
int
main(){
内伦;
字符行[MAXLEN];
而((len=getline(line,MAXLEN))){
反向(直线);
printf(“%d-%s”,长,行);
}
返回0;
}
/*
*从标准输入读取一行
*/
int getline(字符s[],int len){
int c,i;
对于(i=0;i 对于(k=0;k以下是我的实现。它遵循函数式
#include <stdio.h>
#define MAXLINE 1000
int getline(char org[], int maxline);
void reverse(char rev[], char org[], int len);
int main(void) {
char org[MAXLINE];
char rev[MAXLINE];
int len;
while ((len = getline(org, MAXLINE)) > 0) {
if (len > 0) {
reverse(rev, org, len);
printf("%s", rev);
}
}
return 0;
}
int getline(char org[], int maxline) {
int i = 0, c;
for (i = 0; i < maxline - 1 && (c = getchar()) != EOF && c != '\n'; i++) {
org[i] = c;
}
if (c == '\n') {
org[i] = '\n';
i++;
}
org[i] = '\0';
return i;
}
void reverse(char rev[], char org[], int len) {
int i;
for (i = 0; i < len; i++) {
rev[len - i] = org[i];
}
rev[0] = '\b';
rev[1] = '\b';
rev[len + 1] = '\n';
rev[len + 2] = '\0';
}
#包括
#定义MAXLINE 1000
int getline(字符组织[],int maxline);
无效反向(字符版本[],字符组织[],内部长度);
内部主(空){
字符组织[MAXLINE];
char rev[MAXLINE];
内伦;
而((len=getline(org,MAXLINE))>0){
如果(len>0){
反面(版次、组织、长度);
printf(“%s”,版次);
}
}
返回0;
}
int getline(字符组织[],int maxline){
int i=0,c;
对于(i=0;i
我看不到任何函数reverse()
,事实上reverses()
行[len-j]
是j==0
时结尾的空字符,因此您的反向字符串是空的。您可能想要j
而不是j
for(j=0;j<=len;j++){
temp = &line[len-j];
line[j] = *temp;
}
#include <stdio.h>
/* exercise 1.19 */
#define MAXLEN 1000
int getline(char s[], int len);
void reverse(char s[]);
int
main(){
int len;
char line[MAXLEN];
while ((len=getline(line,MAXLEN))){
reverse(line);
printf("%d - %s",len, line);
}
return 0;
}
/*
* Reads a line from stdin
*/
int getline(char s[], int len){
int c, i;
for (i = 0; i < len -1 && (c=getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if (c == '\n'){
s[i] = '\n';
++i;
}
s[i]='\0';
return i;
}
/*
* Reverses the order of chracters in string s
*/
void reverse(char s[]){
int i, k;
char tmp[MAXLEN];
i = 0;
/* copy the character string */
while ((tmp[i] = s[i]) != '\0')
++i;
--i; /* last character is nil */
for (k = 0; k <= i ; ++k)
s[k] = tmp[i-k];
s[k] = '\0';
}
#include <stdio.h>
#define MAXLINE 1000
int getline(char org[], int maxline);
void reverse(char rev[], char org[], int len);
int main(void) {
char org[MAXLINE];
char rev[MAXLINE];
int len;
while ((len = getline(org, MAXLINE)) > 0) {
if (len > 0) {
reverse(rev, org, len);
printf("%s", rev);
}
}
return 0;
}
int getline(char org[], int maxline) {
int i = 0, c;
for (i = 0; i < maxline - 1 && (c = getchar()) != EOF && c != '\n'; i++) {
org[i] = c;
}
if (c == '\n') {
org[i] = '\n';
i++;
}
org[i] = '\0';
return i;
}
void reverse(char rev[], char org[], int len) {
int i;
for (i = 0; i < len; i++) {
rev[len - i] = org[i];
}
rev[0] = '\b';
rev[1] = '\b';
rev[len + 1] = '\n';
rev[len + 2] = '\0';
}