Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Practical C Programming
  • Table Of Contents Toc
Practical C Programming

Practical C Programming

By : B. M. Harwani
2.5 (2)
close
close
Practical C Programming

Practical C Programming

2.5 (2)
By: B. M. Harwani

Overview of this book

Used in everything from microcontrollers to operating systems, C is a popular programming language among developers because of its flexibility and versatility. This book helps you get hands-on with various tasks, covering the fundamental as well as complex C programming concepts that are essential for making real-life applications. You’ll start with recipes for arrays, strings, user-defined functions, and pre-processing directives. Once you’re familiar with the basic features, you’ll gradually move on to learning pointers, file handling, concurrency, networking, and inter-process communication (IPC). The book then illustrates how to carry out searching and arrange data using different sorting techniques, before demonstrating the implementation of data structures such as stacks and queues. Later, you’ll learn interesting programming features such as using graphics for drawing and animation, and the application of general-purpose utilities. Finally, the book will take you through advanced concepts such as low-level programming, embedded software, IoT, and security in coding, as well as techniques for improving code performance. By the end of this book, you'll have a clear understanding of C programming, and have the skills you need to develop robust apps.
Table of Contents (20 chapters)
close
close

Determining whether the string is a palindrome 

A palindrome is a string that reads the same regardless of whether it is in a forward or backwards order. For example, the word radar is a palindrome because it reads the same way forwards and backwards.

How to do it…

  1. Define two 80-character strings called str and rev(assuming your string will not exceed 79 characters). Your string can be of any length, but remember that the last position in the string is fixed for the null character \0:
char str[80],rev[80];
  1. Enter characters that will be assigned to the str string:
printf("Enter a string: ");
scanf("%s",str);
  1. Compute the length of the string using the strlen function and assign this to the n variable:
n=strlen(str);
  1. Execute a for loop in reverse order to access the characters in the str string in reverse order, and then assign them to the rev string:
for(i=n-1;i >=0;  i--)
{
rev[x]=str[i];
x++;
}
rev[x]='\0';
  1. Compare the two strings, str and rev, using strcmp
if(strcmp(str,rev)==0)
  1. If str and rev are the same, then the string is a palindrome.
In C, the functionality of specific built-in functions is specified in the respective libraries, also known as header files. So, while writing C programs, whenever built-in functions are used, we need to use their respective header files in the program at the top. The header files usually have the extension .h. In the following program, I am using a built-in function called strlen, which finds out the length of a string. Therefore, I need to use its library, string.h, in the program.

The palindrome.c program for finding out whether the specified string is a palindrome is as follows:

#include<stdio.h>  
#include<string.h>
void main()
{
char str[80],rev[80];
int n,i,x;
printf("Enter a string: ");
scanf("%s",str);
n=strlen(str);
x=0;
for(i=n-1;i >=0; i--)
{
rev[x]=str[i];
x++;
}
rev[x]='\0';
if(strcmp(str,rev)==0)
printf("The %s is palindrome",str);
else
printf("The %s is not palindrome",str);
}

Now, let's go behind the scenes to understand the code better.

How it works...

To ensure that a string is a palindrome, we first need to ensure that the original string and its reverse form are of the same length.

Let's suppose that the original string is sanjay and it is assigned to a string variable, str. The string is a character array, where each character is stored individually as an array element and the last element in the string array is a null character. The null character is represented as \0 and is always the last element in a string variable in C, as shown in the following diagram:

Figure 2.1

As you can see, the string uses zero-based indexing, that is, the first character is placed at index location str[0], followed by the second character at str[1], and so on. In regards to the last element, the null character is at str[6].

Using the strlen library function, we will compute the length of the entered string and assign it to the n variable. By executing the for loop in reverse order, each of the characters of the str string is accessed in reverse orderthat is, from n-1 to 0, and assigned to the rev string.

Finally, a null character, \0, is added to the rev string to make it a complete string. Therefore, rev will contain the characters of the str string, but in reverse order:

Figure 2.2

Next, we will run the strcmp function. If the function returns 0, it means that the content in the str and rev strings is exactly the same, which means that str is a palindrome. If the strcmp function returns a value other than 0, it means that the two strings are not the same; hence, str is not a palindrome. 

Let's use GCC to compile the palindrome.c program, as follows:

D:\CBook>gcc palindrome.c -o palindrome

Now, let's run the generated executable file, palindrome.exe, to see the output of the program:

D:\CBook>./palindrome
Enter a string: sanjay
The sanjay is not palindrome

Now, suppose that str is assigned another character string, sanas. To ensure that the word in str is a palindrome, we will again reverse the character order in the string.

So, once more, we will compute the length of str, execute a for loop in reverse order, and access and assign each character in str to rev. The null character \0 will be assigned to the last location in rev, as follows:

Figure 2.3

Finally, we will invoke the strcmp function again and supply both strings. 

After compiling, let's run the program again with the new string:

D:\CBook>palindrome
Enter a string: sanas
The sanas is palindrome

Voilà! We have successfully identified whether our character strings are palindromes. Now, let's move on to the next recipe!

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Practical C Programming
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon