Objective of this program is to expand the given string if it
contains a range, for example a-f, 0-5.
Following are considered valid range expressions:
1. lower case letters between a and z. Ex: a-f, d-i
2. Upper case letters between A and Z. Ex: A-F, D-I
3. Numbers between 0 and 9. Ex: 1-5
4. All the above 3 in the reverse order. Ex: f-a, F-A, 3-0
In case of invalid sequences in the input string, sequence
should be copied as is.
------------------------------ --------
Sample Input Sample Output
------------------------------ --------
a-fuy0-3ZA-F abcdefuy0123ZABCDEF
a-f6-3F-9 abcdef6543F-9
-ABCD- -ABCD-
------------------------------ -------
Program:
#include<stdio.h>
int main()
{
int l,m,n,f,g;
char a[1000];
int i;
int j;
scanf("%s",a);
int valid,validr;
i=0;
while(a[i]!='\0')
{
printf("%c",a[i]);
i++;
while((a[i]!='-')&&(a[i]!='\0'))
{
printf("%c",a[i]);
i++;
}
if(a[i]!='\0')
{
f=i-1;g=i+1;
valid=0;validr=0;
if(a[f]>='A' && ((a[g]>='A')&&(a[g]>=a[f])))
valid=1;
else if((a[f]>='a' && ((a[g]>='a')&&(a[g]>=a[f]))))
valid=1;
else if(a[f]>='0' && ((a[g]>='0')&&(a[g]>=a[f])))
valid=1;
else if((a[g]>='A' && ((a[f]>='A')&&(a[f]<=a[g]))))
validr=1;
else if((a[g]>='a' && ((a[f]>='a')&&(a[f]<=a[g]))))
validr=1;
else if((a[g]>='0' && ((a[f]<='9')&&(a[f]>=a[g]))))
validr=1;
if(valid)
{
for(j=a[i-1]+1;j<a[i+1];j++)
printf("%c",j);
i++;
}
else if(validr)
{
for(j=a[i-1]-1;j>a[i+1];j--)
printf("%c",j);
i++;
}
}
else break;
}
return 0;
}
contains a range, for example a-f, 0-5.
Following are considered valid range expressions:
1. lower case letters between a and z. Ex: a-f, d-i
2. Upper case letters between A and Z. Ex: A-F, D-I
3. Numbers between 0 and 9. Ex: 1-5
4. All the above 3 in the reverse order. Ex: f-a, F-A, 3-0
In case of invalid sequences in the input string, sequence
should be copied as is.
------------------------------
Sample Input Sample Output
------------------------------
a-fuy0-3ZA-F abcdefuy0123ZABCDEF
a-f6-3F-9 abcdef6543F-9
-ABCD- -ABCD-
------------------------------
Program:
#include<stdio.h>
int main()
{
int l,m,n,f,g;
char a[1000];
int i;
int j;
scanf("%s",a);
int valid,validr;
i=0;
while(a[i]!='\0')
{
printf("%c",a[i]);
i++;
while((a[i]!='-')&&(a[i]!='\0'))
{
printf("%c",a[i]);
i++;
}
if(a[i]!='\0')
{
f=i-1;g=i+1;
valid=0;validr=0;
if(a[f]>='A' && ((a[g]>='A')&&(a[g]>=a[f])))
valid=1;
else if((a[f]>='a' && ((a[g]>='a')&&(a[g]>=a[f]))))
valid=1;
else if(a[f]>='0' && ((a[g]>='0')&&(a[g]>=a[f])))
valid=1;
else if((a[g]>='A' && ((a[f]>='A')&&(a[f]<=a[g]))))
validr=1;
else if((a[g]>='a' && ((a[f]>='a')&&(a[f]<=a[g]))))
validr=1;
else if((a[g]>='0' && ((a[f]<='9')&&(a[f]>=a[g]))))
validr=1;
if(valid)
{
for(j=a[i-1]+1;j<a[i+1];j++)
printf("%c",j);
i++;
}
else if(validr)
{
for(j=a[i-1]-1;j>a[i+1];j--)
printf("%c",j);
i++;
}
}
else break;
}
return 0;
}
1 comment:
if we give 'd-a' as input its not coming... can anybody help??
Post a Comment