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 : Harwani
2.5 (2)
close
close
Practical C Programming

Practical C Programming

2.5 (2)
By: 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

Finding the occurrence of the first repetitive character in a string

In this recipe, you will learn how to create a program that displays the first character to be repeated in a string. For example, if you enter the string racecar, the program should give the output as The first repetitive character in the string racecar is c. The program should display No character is repeated in the string if a string with no repetitive characters is entered.

How to do it…

  1. Define two strings called str1 and str2. Your strings can be of any length, but the last position in the string is fixed for the null character \0:
char str1[80],str2[80];
  1. Enter characters to be assigned to str1. The characters will be assigned to the respective index locations of the string, beginning with str1[0]:
printf("Enter a string: ");
scanf("%s",str1);
  1. Compute the length of str1 using the strlen library function. Here, the first character of str1 is assigned to str2:
n=strlen(str1);
str2[0]=str1[0];
  1. Use a for loop to access all of the characters of str1 one by one and pass them to the ifexists function to check whether that character already exists in str2. If the character is found in str2, this means it is the first repetitive character of the string, and so it is displayed on the screen:
for(i=1;i < n; i++)
{
if(ifexists(str1[i], str2, x))
{
printf("The first repetitive character in %s is %c", str1,
str1[i]);
break;
}
}
  1. If the character of str1 does not exist in str2, then it is simply added to str2:
else
{
str2[x]=str1[i];
x++;
}

The repetitive.c program for finding the occurrence of the first repetitive character in a string is as follows::

#include<stdio.h>  
#include<string.h>
int ifexists(char u, char z[], int v)
{
int i;
for (i=0; i<v;i++)
if (z[i]==u) return (1);
return (0);
}

void main()
{
char str1[80],str2[80];
int n,i,x;
printf("Enter a string: ");
scanf("%s",str1);
n=strlen(str1);
str2[0]=str1[0];
x=1;
for(i=1;i < n; i++)
{
if(ifexists(str1[i], str2, x))
{
printf("The first repetitive character in %s is %c", str1,
str1[i]);
break;
}
else
{
str2[x]=str1[i];
x++;
}
}
if(i==n)
printf("There is no repetitive character in the string %s", str1);
}

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

How it works...

Let's assume that we have defined a string, str1, of some length, and have entered the following characters—racecar.

Each of the characters of the string racecar will be assigned to the respective index locations of str1, that is, r will be assigned to str1[0]a will be assigned to str1[1], and so on. Because every string in C is terminated by a null character, \0, the last index location of str1 will have the null character \0, as follows:

Figure 2.4

Using the library function strlen, the length of str1 is computed and a for loop is used to access all of the characters of str1, one by one, except for the first character. The first character is already assigned to str2, as shown in the following diagram:

Figure 2.5

Each character that is accessed from str1 is passed through the ifexists function. The ifexists function will check whether the supplied character already exists in str2 and will return a Boolean value accordingly. The function returns 1, that is, true, if the supplied character is found in str2. The function returns 0, that is, false, if the supplied character is not found in str2.

If ifexists returns 1, this means that the character is found in str2, and hence, the first repetitive character of the string is displayed on the screen. If the ifexists function returns 0, this means that the character does not exist in str2, so it is simply added to str2 instead.

Since the first character is already assigned, the second character of str1 is picked up and checked to see if it already exists in str2. Because the second character of str1 does not exist in str2, it is added to the latter string, as follows:

Figure 2.6

The procedure is repeated until all of the characters of str1 are accessed. If all the characters of str1 are accessed and none of them are found to exist in str2, this means that all of the characters in str1 are unique and none of them are repeated.

The following diagram shows strings str1 and str2 after accessing the first four characters of str1. You can see that the four characters are added to str2, since none of them already exist in str2:

Figure 2.7

The next character to be accessed from str1 is c. Before adding it to str2, it is compared with all the existing characters of str2 to determine if it already exists there. Because the c character already exists in str2, it is not added to str2 and is declared as the first repeating character in str1, as follows:

Figure 2.8

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

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

Let's run the generated executable file, repetitive.exe, to see the output of the program:

D:\CBook>./repetitive
Enter a string: education
There is no repetitive character in the string education

Let's run the program again:

D:\CBook>repetitive
Enter a string: racecar
The first repetitive character in racecar is c

Voilà! We've successfully found the first repeating character in a string. 

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