logo

C에서 문자열 뒤집기

이 항목에서는 C 프로그래밍 언어에서 문자열을 반전시키는 여러 가지 방법에 대해 설명합니다. 문자열 반전 문자열의 마지막 문자가 문자열의 첫 번째 문자가 되도록 주어진 문자열의 순서를 바꾸거나 변경하는 기술입니다. 또한, 원래 문자열을 뒤집어서 주어진 문자열의 Palindrome을 확인할 수도 있습니다.

예를 들어 '라는 문자열을 입력합니다. 사과 '를 선택한 다음 역방향 알고리즘을 사용합니다. 역방향 알고리즘은 ' 문자열을 반환합니다. 엘파 ' 이는 원래 문자열과 완전히 반대입니다.

C에서 문자열 뒤집기

C에서 문자열의 역순을 찾는 다양한 방법

다음은 C 프로그래밍 언어에서 문자열의 역방향을 찾는 다양한 방법입니다.

  1. strrev() 함수를 사용하여 문자열 반전
  2. 라이브러리 함수를 사용하지 않고 문자열 반전
  3. 재귀 함수를 사용하여 문자열 뒤집기
  4. for 루프를 사용하여 문자열 뒤집기
  5. while 루프를 사용하여 문자열 반전
  6. 포인터를 사용하여 문자열 반전
  7. Palindrome을 확인하기 위해 문자열을 뒤집습니다.

프로그램 1: strrev() 함수를 사용하여 문자열의 반전을 인쇄합니다.

strrev() 함수를 사용하여 문자열의 반대를 인쇄하는 예를 고려해 보겠습니다.

프로그램1.c

자바 유형 변수
 #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; } 

산출

 Enter a string to be reversed: AMBULANCE After the reverse of a string: ECNALUBMA 

프로그램 2: 라이브러리 함수를 사용하지 않고 문자열의 반전을 인쇄합니다.

사용자 정의 함수를 사용하여 문자열의 반대를 인쇄하는 예를 고려해 보겠습니다.

프로그램2.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&apos;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&apos;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 (&apos; 
 Display a reverse string in the C: 
&apos;); printf (&apos; ----------------------- &apos;); printf (&apos; 
 Enter a string to reverse order: &apos;); scanf( &apos;%s&apos;, &amp;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&apos;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 (&apos; Enter a string to be reversed: &apos;); scanf( &apos;%s&apos;, 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&apos;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 (&apos; Enter a string to be reversed: &apos;); scanf( &apos;%s&apos;, st); revstr(st); printf (&apos; The reverse string is: %s&apos;, 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) !="" ) i++; return i; pre> <p> <strong>Output</strong> </p> <pre> Enter a string to be reversed: JAVATPOINT The reverse string is: TNIOPTAVAJ </pre> <h3>Program 7: Program to check whether the reverse string is a Palindrome</h3> <p>Consider a program to check whether the given string is a palindrome or not in the C programming language.</p> <p> <strong>Program7.c</strong> </p> <pre> #include #include int main () { // declare variables char str1[30]; int i, len, flag = 0; printf (&apos; Enter a string: &apos;); scanf (&apos;%s&apos;, str1); len = strlen( str1 ); // get the string length for ( i = 0; i <len; i++) { str1[i] is not equal to str1[len-i-1] if (str1[i] !="str1[len" - i 1]) flag="1;" break; exit from statement } (flag) printf (' %s a palindrome string', str1); else palindrome', return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a string: madam madam is a palindrome string. </pre> <hr></len;></pre></len></pre></j)></pre></right;></pre></len></pre></len>

프로그램 3: 재귀 함수를 사용하여 문자열의 역순을 인쇄합니다.

재귀 함수를 사용하여 문자열의 반대를 인쇄하는 예를 고려해 보겠습니다.

재귀 함수: 재귀 함수는 반복문을 사용하지 않고 동일한 함수를 계속해서 호출하는 함수입니다.

CSS 배경

프로그램3.c

 #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&apos;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 (&apos; 
 Display a reverse string in the C: 
&apos;); printf (&apos; ----------------------- &apos;); printf (&apos; 
 Enter a string to reverse order: &apos;); scanf( &apos;%s&apos;, &amp;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&apos;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 (&apos; Enter a string to be reversed: &apos;); scanf( &apos;%s&apos;, 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&apos;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 (&apos; Enter a string to be reversed: &apos;); scanf( &apos;%s&apos;, st); revstr(st); printf (&apos; The reverse string is: %s&apos;, 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) !="" ) i++; return i; pre> <p> <strong>Output</strong> </p> <pre> Enter a string to be reversed: JAVATPOINT The reverse string is: TNIOPTAVAJ </pre> <h3>Program 7: Program to check whether the reverse string is a Palindrome</h3> <p>Consider a program to check whether the given string is a palindrome or not in the C programming language.</p> <p> <strong>Program7.c</strong> </p> <pre> #include #include int main () { // declare variables char str1[30]; int i, len, flag = 0; printf (&apos; Enter a string: &apos;); scanf (&apos;%s&apos;, str1); len = strlen( str1 ); // get the string length for ( i = 0; i <len; i++) { str1[i] is not equal to str1[len-i-1] if (str1[i] !="str1[len" - i 1]) flag="1;" break; exit from statement } (flag) printf (\' %s a palindrome string\', str1); else palindrome\', return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a string: madam madam is a palindrome string. </pre> <hr></len;></pre></len></pre></j)></pre></right;></pre></len>

프로그램 4: for 루프를 사용하여 문자열의 역순을 인쇄합니다.

C 프로그래밍 언어에서 for 루프를 사용하여 문자열의 역순을 인쇄하는 예를 고려해 보겠습니다.

프로그램4.c

 #include #include #include void main() { char str[50], temp; // define the size of str[] array int i, left, right, len; printf (&apos; 
 Display a reverse string in the C: 
&apos;); printf (&apos; ----------------------- &apos;); printf (&apos; 
 Enter a string to reverse order: &apos;); scanf( &apos;%s&apos;, &amp;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&apos;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 (&apos; Enter a string to be reversed: &apos;); scanf( &apos;%s&apos;, 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&apos;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 (&apos; Enter a string to be reversed: &apos;); scanf( &apos;%s&apos;, st); revstr(st); printf (&apos; The reverse string is: %s&apos;, 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) !="" ) i++; return i; pre> <p> <strong>Output</strong> </p> <pre> Enter a string to be reversed: JAVATPOINT The reverse string is: TNIOPTAVAJ </pre> <h3>Program 7: Program to check whether the reverse string is a Palindrome</h3> <p>Consider a program to check whether the given string is a palindrome or not in the C programming language.</p> <p> <strong>Program7.c</strong> </p> <pre> #include #include int main () { // declare variables char str1[30]; int i, len, flag = 0; printf (&apos; Enter a string: &apos;); scanf (&apos;%s&apos;, str1); len = strlen( str1 ); // get the string length for ( i = 0; i <len; i++) { str1[i] is not equal to str1[len-i-1] if (str1[i] !="str1[len" - i 1]) flag="1;" break; exit from statement } (flag) printf (\' %s a palindrome string\', str1); else palindrome\', return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a string: madam madam is a palindrome string. </pre> <hr></len;></pre></len></pre></j)></pre></right;>

프로그램 5: while 루프를 사용하여 문자열의 역순을 인쇄합니다.

C 프로그래밍 언어에서 while 루프를 사용하여 문자열의 역순을 인쇄하는 예를 고려해 보겠습니다.

프로그램5.c

 #include #include int main() { char str1[50], temp; // declare and initialize the size of string array. int i = 0, j =0; printf (&apos; Enter a string to be reversed: &apos;); scanf( &apos;%s&apos;, 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&apos;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 (&apos; Enter a string to be reversed: &apos;); scanf( &apos;%s&apos;, st); revstr(st); printf (&apos; The reverse string is: %s&apos;, 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) !="" ) i++; return i; pre> <p> <strong>Output</strong> </p> <pre> Enter a string to be reversed: JAVATPOINT The reverse string is: TNIOPTAVAJ </pre> <h3>Program 7: Program to check whether the reverse string is a Palindrome</h3> <p>Consider a program to check whether the given string is a palindrome or not in the C programming language.</p> <p> <strong>Program7.c</strong> </p> <pre> #include #include int main () { // declare variables char str1[30]; int i, len, flag = 0; printf (&apos; Enter a string: &apos;); scanf (&apos;%s&apos;, str1); len = strlen( str1 ); // get the string length for ( i = 0; i <len; i++) { str1[i] is not equal to str1[len-i-1] if (str1[i] !="str1[len" - i 1]) flag="1;" break; exit from statement } (flag) printf (\' %s a palindrome string\', str1); else palindrome\', return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a string: madam madam is a palindrome string. </pre> <hr></len;></pre></len></pre></j)>

프로그램 6: 포인터를 사용하여 문자열의 반대방향 인쇄

C 프로그래밍 언어의 포인터를 사용하여 문자열의 반대를 인쇄하는 예를 고려해 보겠습니다.

mylivecricket 대안

프로그램6.c

 #include #include int str_len( char *st); void revstr( char *st); int main() { char st[50]; printf (&apos; Enter a string to be reversed: &apos;); scanf( &apos;%s&apos;, st); revstr(st); printf (&apos; The reverse string is: %s&apos;, 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) !="" ) i++; return i; pre> <p> <strong>Output</strong> </p> <pre> Enter a string to be reversed: JAVATPOINT The reverse string is: TNIOPTAVAJ </pre> <h3>Program 7: Program to check whether the reverse string is a Palindrome</h3> <p>Consider a program to check whether the given string is a palindrome or not in the C programming language.</p> <p> <strong>Program7.c</strong> </p> <pre> #include #include int main () { // declare variables char str1[30]; int i, len, flag = 0; printf (&apos; Enter a string: &apos;); scanf (&apos;%s&apos;, str1); len = strlen( str1 ); // get the string length for ( i = 0; i <len; i++) { str1[i] is not equal to str1[len-i-1] if (str1[i] !="str1[len" - i 1]) flag="1;" break; exit from statement } (flag) printf (\' %s a palindrome string\', str1); else palindrome\', return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a string: madam madam is a palindrome string. </pre> <hr></len;></pre></len>

프로그램 7: 역방향 문자열이 회문인지 확인하는 프로그램

C 프로그래밍 언어에서 주어진 문자열이 회문인지 여부를 확인하는 프로그램을 고려하십시오.

프로그램7.c

 #include #include int main () { // declare variables char str1[30]; int i, len, flag = 0; printf (&apos; Enter a string: &apos;); scanf (&apos;%s&apos;, str1); len = strlen( str1 ); // get the string length for ( i = 0; i <len; i++) { str1[i] is not equal to str1[len-i-1] if (str1[i] !="str1[len" - i 1]) flag="1;" break; exit from statement } (flag) printf (\\' %s a palindrome string\\', str1); else palindrome\\', return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter a string: madam madam is a palindrome string. </pre> <hr></len;>