Šajā tēmā tiks apspriesti vairāki veidi, kā apgriezt virkni C programmēšanas valodā. Virknes apgriešana ir paņēmiens, kas apvērš vai maina dotās virknes secību, lai virknes pēdējā rakstzīme kļūtu par virknes pirmo rakstzīmi utt. Turklāt mēs varam arī pārbaudīt dotās virknes palindromu, apgriežot sākotnējo virkni.
Piemēram, mēs ievadām virkni ' ĀBOLS un pēc tam izmantojiet apgriezto algoritmu. Apgrieztais algoritms atgriež virkni ' ELPPA ', kas ir pilnīgi pretējs sākotnējai virknei.
Dažādi veidi, kā atrast virknes reversu C
Tālāk ir norādīti dažādi veidi, kā atrast virknes reversu C programmēšanas valodā:
- Apgrieziet virkni, izmantojot funkciju strrev().
- Apgriezt virkni, neizmantojot bibliotēkas funkciju
- Apgriezt virkni, izmantojot rekursijas funkciju
- Apgrieziet virkni, izmantojot cilpu
- Apgriezt virkni, izmantojot cilpu while
- Apgrieziet virkni, izmantojot norādes
- Apgrieziet virkni, lai pārbaudītu, vai nav palindroms
1. programma: izdrukājiet virknes reversu, izmantojot funkciju strrev ().
Apskatīsim piemēru, kā izdrukāt virknes reversu, izmantojot funkciju strrev().
Programma1.c
java virknes formāts garš
#include #include int main() { char str[40]; // declare the size of character string printf (' Enter a string to be reversed: '); scanf ('%s', str); // use strrev() function to reverse a string printf (' After the reverse of a string: %s ', strrev(str)); return 0; }
Izvade
Enter a string to be reversed: AMBULANCE After the reverse of a string: ECNALUBMA
2. programma: izdrukājiet virknes reversu, neizmantojot bibliotēkas funkciju
Apskatīsim piemēru, kā izdrukāt virknes reversu, izmantojot lietotāja definētu funkciju.
Programma2.c
#include #include // function definition of the revstr() void revstr(char *str1) { // declare variable int i, len, temp; len = strlen(str1); // use strlen() to get the length of str string // use for loop to iterate the string for (i = 0; i <len 2; i++) { temp variable use to temporary hold the string str1[i]="str1[len" - i 1]; str1[len 1]="temp;" } int main() char str[50]; size of printf (' enter string: '); gets(str); gets() function take before reversing %s ', str); call revstr() revstr(str); after %s', < pre> <p> <strong>Output</strong> </p> <pre> Enter the string: Welcome Friends Before reversing the string: Welcome Friends After reversing the string: sdneirF emocleW </pre> <h3>Program 3: Print the reverse of a string using the recursion function</h3> <p>Let's consider an example to print the reverse of a string using the recursion function.</p> <p> <strong>Recursion function:</strong> A recursion function is a function that continuously calls the same function without using a looping statement.</p> <p> <strong>Program3.c</strong> </p> <pre> #include #include // use recursion function void revstr(char *str1) { // declare static variable static int i, len, temp; len = strlen(str1); // use strlen() to get the length of str string if (i <len 2){ temp variable use to temporary hold the string str1[i]="str1[len" - i 1]; str1[len 1]="temp;" i++; revstr(str1); recusively calls revstr() function } int main() { char str1[50]; size of printf (' enter string: '); gets(str1); gets() take before reversing %s ', str1); call after %s', < pre> <p> <strong>Output</strong> </p> <pre> Enter the string: LIFE INSURANCE Before reversing the string: LIFE INSURANCE After reversing the string: ECNARUSNI EFIL </pre> <h3>Program 4: Print the reverse of a string using for loop</h3> <p>Let's consider an example to print the reverse of a string using for loop in C programming language.</p> <p> <strong>Program4.c</strong> </p> <pre> #include #include #include void main() { char str[50], temp; // define the size of str[] array int i, left, right, len; printf (' Display a reverse string in the C: '); printf (' ----------------------- '); printf (' Enter a string to reverse order: '); scanf( '%s', &str); len = strlen(str); // get the length of the string left = 0; // set left index at 0 right = len - 1; // set right index len - 1 // use for loop to store the reverse string for (i = left; i <right; i++) { temp="str[i];" str[i]="str[right];" str[right]="temp;" right--; } printf (' the reverse of original string is: %s ', str); getch(); < pre> <p> <strong>Output</strong> </p> <pre> Display a reverse string in the C: ----------------------- Enter a string to reverse order: APPLE The reverse of the original string is: ELPPA </pre> <h3>Program 5: Print the reverse of a string using while loop</h3> <p>Let's consider an example to print the reverse of a string using while loop in C programming language.</p> <p> <strong>Program5.c</strong> </p> <pre> #include #include int main() { char str1[50], temp; // declare and initialize the size of string array. int i = 0, j =0; printf (' Enter a string to be reversed: '); scanf( '%s', str1); j = strlen (str1) - 1; // get the length of the string // use while loop to define the condition while ( i <j) 1 { use temp variable to store the characters of str1 str1[j]="str1[i];" str1[i]="temp;" i++; i incremented by j--; j is decremented } printf (' reversed string: %s', str1); return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a string to be reversed: JAVATPOINT The reversed of the string: TNIOPTAVAJ </pre> <h3>Program 6: Print the reverse of a string using pointers</h3> <p>Let's consider an example to print the reverse of a string using pointers in the C programming language.</p> <p> <strong>Program6.c</strong> </p> <pre> #include #include int str_len( char *st); void revstr( char *st); int main() { char st[50]; printf (' Enter a string to be reversed: '); scanf( '%s', st); revstr(st); printf (' The reverse string is: %s', st); return 0; } void revstr (char *st) { int len, i; char *start, *end, temp; len = str_len (st); start = st; end = st; for (i = 0; i <len - 1; i++) end++; for (i="0;" i < len 2; { temp="*end;" *end="*start;" *start="temp;" start++; end--; } int str_len (char *ptr) while ( *(ptr + i) !="