Home > Back-end >  Function string reverse (pointer)
Function string reverse (pointer)

Time:06-10

Required to bear on the reverse a string of simple function,

Function interface definition:
Void f (char * p);
Function f to reverse operation of p points to the string, require function f can't define any array, can't call any string handling functions,

The referee sample testing program:
#include
# define MAXS 20

Void f (char * p);
Void ReadString (char * s);/* by the referee, skip some table */

Int main ()
{
Char s [MAXS];

ReadString (s);
F (s);
printf("%s\n", s);

return 0;
}

/* * your code will be embedded in here/
Input the sample:
Hello World!
The output sample:
! DlroW olleH



My code # include & lt; String. H>
Void f (char * p)
{
Int a, b, left, right;
A=strlen (p);
Left=0; Right=a - 1;
While (left{
B=* (p + left); * p=* (p + right); * (p + right)=b;
Left++; Right -;
}
}

CodePudding user response:

B=* (p + left);
* (p + left) =* (p + right);
* (p + right)=b;

CodePudding user response:

While (leftModified into less than or equal to
Solve the odd length error
There are three characters, for example, you try

CodePudding user response:

 # include & lt; stdio.h> 
#include

# define MAXS 20

Void f (char * p);
Void ReadString (char * s);/* by the referee, skip some table */

Int main ()
{
Char s [MAXS];

ReadString (s);
F (s);
printf("%s\n", s);

return 0;
}

Void ReadString (char * s)
{
The scanf (" % s ", s);
}

Void f (char * p)
{
Int len, TMP, left, right;
Len=strlen (p);
Left=0;
Right=len - 1;
While (left{
TMP=* (p + left);
* (p + left)=* (p + right);
* (p=TMP + right);
Left++;
Right -;
}
}

For your reference ~

CodePudding user response:

 
#include
# define MAXS 20

Void f (char * p);
Void ReadString (char * s);/* by the referee, skip some table */

Int main ()
{
Char s [MAXS];

ReadString (s);
F (s);
printf("%s\n", s);

return 0;
}

Void ReadString (char * s)
{
char c;
Char * p;
P=s;
While ((c=fgetc (stdin))! EOF)={
If (c=='\ n' | | c=='\ r) {
* p++='\ 0';
break;
} else {
* p++=c;
}
}
}

Void f (char * p)
{
Char * head, * tail;
char c;

The head=p;
For (tail=head; * tail!='\ 0'; Tail++)
;
Tail -;

For (; Head & lt; Tail; Head++, tail -) {
C=* head;
* head=* tail;
* tail=c;
}
}

CodePudding user response:


I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10581430.html
I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10768339.html
  • Related