instagram youtube
Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
logo
Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Mastering String Palindromes in C

- Team

Jumat, 19 Juli 2024 - 17:26

facebook twitter whatsapp telegram line copy

URL berhasil dicopy

facebook icon twitter icon whatsapp icon telegram icon line icon copy

URL berhasil dicopy


On this article, we will be able to write a C program to test if a string is a palindrome. A string is claimed to be palindrome if it stays the similar on studying from each ends. It signifies that whilst you opposite a given string, it must be the similar as the unique string. As an example, the string ‘degree’ is a palindrome as it stays the similar whilst you learn it from the starting to the top and vice versa. On the other hand, the string ‘Simplilearn’ isn’t a palindrome because the opposite of the string is ‘nraelilpmis,’ which isn’t the identical.

Desire a Best Instrument Building Task? Get started Right here!

Complete Stack Developer – MERN StackDiscover Program

Want a Top Software Development Job? Start Here!

Algorithmic Good judgment of C Program for String Palindrome

To put in writing a C program for string palindrome, we want to practice the good judgment beneath:

  1. Create a serve as to test if the string is a palindrome: isPalindrome(str)
  2. Initialize indexes for high and low ranges to be 0 and n-1, respectively
  3. Till the low index (l) is not up to the prime index (h), do the next:
    1. If str(l) isn’t like str(h), go back false
    2. If str(l) and str(h) are identical, increment l, i.e., l++ and decrement h, i.e., h–
  4. If we achieve this step, it approach there is not any mismatch, and the string is a palindrome; in a different way, if step 3.A is correct, it’s not a palindrome.

Let’s use this good judgment to jot down a C program to test if the given string is a palindrome.

#come with <stdio.h>

#come with <string.h>

// Imposing the good judgment in a serve as

void isPalindrome(char str[]){

// Initializing indexes

int l = 0;

int h = strlen(str) – 1;

// Step 4 to stay checking the characters till they’re identical

whilst (h > l){

if (str[l++] != str[h–]){

printf(“%s isn’t a palindrome stringn”, str);

go back;

}

}

printf(“%s is a palindrome stringn”, str);

}

// Driving force program

int major(){

isPalindrome(“degree”);

isPalindrome(“radar”);

isPalindrome(“Simplilearn”);

go back 0;

}

Output:

C_Program_for_String_Palindrome_1.

As you’ll see within the above output, the strings ‘degree’ and ‘radar’ that stay the similar after reversing are displayed as a palindrome. On the other hand, for this situation, we gave the strings ourselves. Within the coming examples, we will be able to ask the consumer to go into the string and write a C program to test a string palindrome the usage of quite a lot of strategies.

Desire a Best Instrument Building Task? Get started Right here!

Complete Stack Developer – MERN StackDiscover Program

Want a Top Software Development Job? Start Here!

C Program for String Palindrome The use of the Same old Means

We will be able to write a C program for string palindrome the usage of an ordinary way with none serve as or libraries. On this instance, we will be able to:

  1. Learn the string entered by way of the consumer the usage of will get(s)
  2. Calculate the period and retailer it in a variable
  3. Initialize low and high indexes
  4. Evaluate the characters at high and low index
  5. Use for loop to stay evaluating till a mismatch is located

Right here’s the implementation of checking a string palindrome the usage of an ordinary way.

#come with <stdio.h>

#come with <string.h>

int major(){

    char str[1000];  

    int l,n,comp=0; 

    printf(“Input the string to test : “);

    will get(str);

    n=strlen(str); 

    for(l=0;l<n/2;l++){

     if(str[l]==str[n-l-1])

     comp++;

  }

  if(comp==l)

      printf(“The entered string is a palindrome”);

    else

        printf(“The entered string isn’t a palindrome”);

    go back 0;

}

Output:

C_Program_for_String_Palindrome_2

Output:

C_Program_for_String_Palindrome_3

Desire a Best Instrument Building Task? Get started Right here!

Complete Stack Developer – MERN StackDiscover Program

Want a Top Software Development Job? Start Here!

C Program for String Palindrome The use of a Serve as

The good judgment for writing a C program for string palindrome the usage of a serve as is sort of the similar as that whilst the usage of an ordinary way. The numerous distinction is this time we will be able to create a serve as for checking palindrome. Additionally, after studying the string the usage of will get(s), we will be able to go it as an issue to the serve as. 

Right here’s the C program to test if a string is a palindrome the usage of a serve as.

#come with <stdio.h>

#come with <string.h>

// Growing the serve as

int isPalindrome(char *str){

    int l,comp=0,n;

    n=strlen(str);  

for(l=0;l<n/2;l++){

     if(str[l]==str[n-l-1])

     comp++;

  } 

  if(comp==l)

        go back 1;

    else

        go back 0;

 }

int major(){    

    char str[1000];

    printf(“Input the string to test: “);

    will get(str); 

    if(isPalindrome(str))

      printf(“The entered string is a palindrome”);

    else

        printf(“The entered string isn’t a palindrome”);        

    go back 0;

}

Output:

C_Program_for_String_Palindrome_4.

Output:

C_Program_for_String_Palindrome_5

Desire a Best Instrument Building Task? Get started Right here!

Complete Stack Developer – MERN StackDiscover Program

Want a Top Software Development Job? Start Here!

C Program for String Palindrome The use of Recursion

The set of rules for writing a C program for string palindrome is identical with just a unmarried main distinction: the serve as will stay calling itself recursively till the decrease index isn’t not up to part the period of the string, i.e., l<n/2. For the reason that serve as will stay calling itself recursively, we will be able to now not have to make use of the for loop this time.

Instance

#come with <stdio.h>

#come with <string.h>

void take a look at(char [], int);

int major(){

    char str[15];

    printf(“Input a phrase to test: “);

    scanf(“%s”, str);

    isPalindrome(str, 0);

    go back 0;

void isPalindrome(char str[], int l){

    int n = strlen(str) – (l + 1);   

    if (str[l] == str[n]){        

        if (l + 1 == n || l == n){

            printf(“The entered phrase is a palindromen”);

            go back;

        }      

        isPalindrome(str, l + 1);

    }

    else

    {

        printf(“The entered phrase isn’t a palindromen”);

    }

}

Output:

C_Program_for_String_Palindrome_6.

Output:

C_Program_for_String_Palindrome_7.

C Program for String Palindrome The use of String Library Purposes

For this situation, we will be able to be the usage of 3 string library purposes outlined within the string.h header document: strcpy, strrev, and strcmp. Those 3 purposes will permit us to replicate, opposite, and evaluate strings, the 3 necessities for checking a palindrome string. 

Right here’s the C program for string palindrome the usage of string library purposes.

#come with <stdio.h>

#come with <string.h>

int major(){

char inArray[100], revArray[100];

printf(“Input the string to test: “);

    scanf(“%s”, inArray);  

// Copying enter string

    strcpy(revArray, inArray);   

// Reversing the string

    strrev(revArray);

// Evaluating the reversed string with enter string

if(strcmp(inputArray, reversedArray) == 0 )

printf(“%s is a palindrome.n”, inputArray);

else

printf(“%s isn’t a palindrome.n”, inputArray);       

getch();

go back 0;

}

Output:

C_Program_for_String_Palindrome_8.

Be aware: The above program makes use of the strrev() serve as, which is to be had simplest in ANSI C (Turbo C/C++ compilers). Therefore, if you’re the usage of an ordinary GCC compiler, the above code would possibly throw an error.

Conclusion

On this article, you’ve got realized easy methods to write a C program for string palindrome in several tactics. You’ll be able to additionally write it the usage of guidelines in C. You’ll be able to additionally take a look at writing a program in C++ to test a string palindrome. C++ is a longer model of C programming. In case you are new to C++, you’ll check with Simplilearn’s C++ Educational for Learners to get a transparent figuring out of the entire basics. But even so that, you’ll additionally join our SkillUp platform. The platform provides a large number of loose lessons in several programming languages to reinforce your talents in any programming language, together with C and C++.

You’ll be able to additionally opt for our Complete Stack Developer – MERN Stack, a certification route that gives coaching in virtually 30 programming languages and gear. To position it merely, it help you get the mastery in more than one building talents required to grow to be a full-stack developer and land a possibility to paintings for the most important firms within the device building international.

supply: www.simplilearn.com

Berita Terkait

Most sensible Recommended Engineering Tactics | 2025
Unfastened Flow Vs General Flow
Be told How AI Automation Is Evolving in 2025
What Is a PHP Compiler & The best way to use it?
Best Leadership Books You Should Read in 2024
Best JavaScript Examples You Must Try in 2025
How to Choose the Right Free Course for the Best Value of Time Spent
What Is Product Design? Definition & Key Principles
Berita ini 7 kali dibaca

Berita Terkait

Selasa, 11 Februari 2025 - 22:32

Revo Uninstaller Pro 5.3.5

Selasa, 11 Februari 2025 - 22:21

Rhinoceros 8.15.25019.13001

Selasa, 11 Februari 2025 - 22:12

Robin YouTube Video Downloader Pro 6.11.10

Selasa, 11 Februari 2025 - 22:08

RoboDK 5.9.0.25039

Selasa, 11 Februari 2025 - 22:05

RoboTask 10.2.2

Selasa, 11 Februari 2025 - 21:18

Room Arranger 10.0.1.714 / 9.6.2.625

Selasa, 11 Februari 2025 - 17:14

Team11 v1.0.2 – Fantasy Cricket App

Selasa, 11 Februari 2025 - 16:20

Sandboxie 1.15.6 / Classic 5.70.6

Berita Terbaru

Headline

Revo Uninstaller Pro 5.3.5

Selasa, 11 Feb 2025 - 22:32

Headline

Rhinoceros 8.15.25019.13001

Selasa, 11 Feb 2025 - 22:21

Headline

Robin YouTube Video Downloader Pro 6.11.10

Selasa, 11 Feb 2025 - 22:12

Headline

RoboDK 5.9.0.25039

Selasa, 11 Feb 2025 - 22:08

Headline

RoboTask 10.2.2

Selasa, 11 Feb 2025 - 22:05