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.