First Repeating Character From Last
A string S is passed as the input. S has at least one repeating character. The program must print the first repeating character C from the last.
Input Format:
The first line contains S.
The first line contains S.
Output Format:
The first line contains C.
The first line contains C.
Boundary Conditions:
Length of S will be from 3 to 100.
Length of S will be from 3 to 100.
Example Input/Output 1:
Input:
abcdexyzbwqpooplj
Input:
abcdexyzbwqpooplj
Output:
p
p
Code:
#include
<bits/stdc++.h>
using namespace std;
int main(int argc, char**
argv)
{
char str[100];
fgets(str,100,stdin);
int i,j,flag=0;
char temp;
for(i=strlen(str)-1;i>=0;i--)
{
temp=str[i];
for(j=i-1;j>=0;j--)
{
if(temp==str[j])
{
cout<<temp;
flag=1;
break;
}
}
if(flag==1)
break;
}
}
Please do comment If u have any Queries!
#include
ReplyDelete#include
int main()
{
char s[100]={'\0'};
scanf("%[^\n]%*s",&s);//include this scanf to get the program work correctly for string with spaces
int l=strlen(s);
int j=0,found=0;
for(int i=l-1;i>=0;i--)
{
for(int j=i-1;j>=0;j--)
{
if(s[i]==s[j] && s[i]!=' ')
{
printf("%c",s[i]);
found=1;
break;
}
}
if(found==1)
break;
}
}