/1.
The contents of one character array are: "Student a Am I", please change the contents of the array to "I am a student".
Requirements:
Library functions cannot be used. Can only open up a finite space (the number of space and the length of the string is independent).
#define _crt_secure_no_warnings #include <stdio.h> #include <stdlib.h> #include <string.h> #include
<assert.h> int Mystrlen (const char *src) {int len = 0;
ASSERT (src!=null);
while (*src++!= ' ") {len++;
return Len;
} void Reverse_str (char *p1, char *p2)//rollover function.
{char ch = 0;
ASSERT (P1!= null && p2!= null);
while (P1 < p2) {ch = *p1;
*P1 = *P2;
*P2 = ch;
p1++;
p2--;
} void Myreverse (char * array) {int len = 0;
char *start = NULL;
char *end = NULL;
ASSERT (array!=null);
Len = Mystrlen (array); Reverse_str (array, array+len-1); Flips the entire string.
The next thing to do is flip each word.
printf ("array =%s\n", array);
while (*array!= ' ")//the cycle is to flip each word.
{start = array; while ((*array!= ') && (*array!= ' ")) {array++;
The Arrray points to a space or is a ' yes '} end = Array-1;
Reverse_str (start, end); if (*array!= ' ")//Here error prone.
Remember to add this condition. {array++; When it points to a space, it further points to the starting position of the new word. But if you go to the tail of the string, array and then Gaga, which causes the outermost loop to not end because it skips the.
int main () {char array[100] = "Student a Am I";
Myreverse (array);
Myreverse (array);
printf ("array =%s\n", array);
printf ("hello...\n");
System ("pause");
return 0; }
2. Implement a function that can be left to rotate the K characters in a string.
AABCD a character to get ABCDA
AABCD L two characters get Bcdaa
#define _crt_secure_no_warnings
#include <stdio.h>
#include <stdlib.h>
#include < string.h>
#include <assert.h>
/method One:
void reverse_ch (char * str, int n)
{
int i = 0;
char *p = NULL;
char *p1 = NULL;
ASSERT (str!= NULL);
p = str;
P1 = str + strlen (str);
n = n% 5; Such a statement, can reduce the cycle many times, improve efficiency. Because the spin 5 times the string becomes the original string.
for (i = 0; i < n; i++) //rotate n characters to the back of the string.
{
*p1++ = *p++;
}
*p1 = ' n ';
while (* (p-n) = *p++)!= ' and ' //move the entire string forward
{
;
}
}
Method two the first n characters are flipped once, the characters remaining in the string are flipped once, and the entire string is flipped one at a time. This method is simple, but n must be less than or equal to strlen (str).
void Myreverse (char *left, char *right)
{
char temp = 0;
ASSERT (left!= null && right!= null);
while (left < right)
{
temp = *left;
*left = *right;
*right = temp;
left++;
right--
}
}
void Reverse_ch3 (char *str, int n) //This method is simple, but n must be less than or equal to strlen (str).
{
assert (str!= NULL);
Myreverse (str,str+n-1);
Myreverse (Str+n,str+strlen (str)-1);
Myreverse (Str,str+strlen (str)-1);
}
int main ()
{
char str[100] = "AABCD";
/*reverse_ch (str, m);
printf ("str =%s\n", str); */
Reverse_ch3 (str, 6);
printf ("str =%s\n", str);
printf ("hello...\n");
System ("pause");
return 0;
}
3.
A string that determines whether a string is rotated after another string.
For example: given S1 = AABCD and S2 = Bcdaa, returns 1, given S1=ABCD and S2=ACBD, and returns 0.
//
//
AABCD a character to get ABCDA
AABCD L two characters get Bcdaa
//
AABCD to the right one character to get DAABC
AABCD right two characters to get Cdaab
#define _crt_secure_no_warnings
#include <stdio.h>
#include <stdlib.h>
#include < string.h>
#include <assert.h>
//Method One: string rotation once judged.
void Reverse_ch1 (char * str, int n)
{
int i = 0;
char *p = NULL;
char *p1 = NULL;
ASSERT (str!= NULL);
p = str;
P1 = str + strlen (str);
for (i = 0; i < n; i++) //rotate n characters to the back of the string.
{
*p1++ = *p++;
}
*p1 = ' n ';
while (* (p-n) = *p++)!= ' and ' //move the entire string forward
{
;
}
}
int Is_revstr (char *str1, char *str2) //str1 source String, str2 is the judge string
{
int len1 = 0;
int len2 = 0;
ASSERT (str1!= null && str2!= null);
Len1 = strlen (str1);
Len2 = strlen (str2);
if (len1!= len2)
{return
0; A string after a string is not rotated by another string.
}
else
{while
(len1 > 0) //source string rotates left len1-1 times to see if the result of each rotation is another string.
{
if (strcmp (str1, str2) = = 0)
{return
1;
}
Reverse_ch1 (str1,1);
len1--
}
}
return 0;
}
Method Two: simple method. Appends the source string to the source character, forming a new string, looking at whether STR2 is a substring of this new string, or, if it is, a flipped string.
int is_revstr2 (char *str1, char *str2)
{
int len1 = strlen (str1);
int len2 = strlen (str2);
ASSERT (str1!= null && str2!= null);
if (len1!= len2)
{return
0; A string after a string is not rotated by another string.
}
Strncat (Str1,str1,strlen (STR1));//appends the source string to the source character, forming a new string, looking at whether STR2 is a substring of this new string and, if it is, a flipped string.
if (strstr (str1, str2)!= NULL) //strncat will automatically add a, this can not use strcat, this will crash.
{return
1;
}
else return
0;
}
int main ()
{
int ret = 0;
Char array[100] = "AABCD";
Char array2[100] = "DAABC";
ret = IS_REVSTR (array,array2);
ret = IS_REVSTR2 (array,array2);
if (ret = 1)
{
printf ("Two strings rotated after");
}
else
{
printf ("Different \ n" after two strings rotated);
}
printf ("hello...\n");
System ("pause");
return 0;
}