Practice Program
Solution:
void main()
{
int i,j;
char ch,s[20];
printf("Enter the string: ");
gets(s);
printf("\nString is :");
puts(s);
for(i=0; s[i] != '\0'; i++)
{
for(j=i+1;s[j] != '\0'; j++)
{
if(s[i] < s[j])//checking a character at index i and j,if character
{//at index i is greater than character at j index, then we are swapping those character.
ch = s[i];
s[i] = s[j];
s[j] = ch;
}
}
}
printf("\nString in Non Alphabatical order is : ");
puts(s);
}
void main() { int i,j; char ch,s[20]; printf("Enter the string: "); gets(s); printf("\nString is :"); puts(s); for(i=0; s[i] != '\0'; i++) { for(j=i+1;s[j] != '\0'; j++) { if(s[i] < s[j])//checking a character at index i and j,if character {//at index i is greater than character at j index, then we are swapping those character. ch = s[i]; s[i] = s[j]; s[j] = ch; } } } printf("\nString in Non Alphabatical order is : "); puts(s); }
Output1
Enter the string: ABCD
String is : ABCD
String in Non Alphabetical order is : DCBA
String is : ABCD
String in Non Alphabetical order is : DCBA
Output2
Enter the string: LMNO
String is :LMNO
String in Non Alphabetical order is : ONML
String is :LMNO
String in Non Alphabetical order is : ONML
Output3
Enter the string: PQRS
String is :PQRS
String in Non Alphabetical order is : SRQP
String is :PQRS
String in Non Alphabetical order is : SRQP
defining a function 'not_dict' which will arrange the character in non alphabetical order.
void not_dict(char s[])
{
int i,j;
char ch;
for(i=0; s[i] != '\0'; i++)
{
for(j=i+1;s[j] != '\0'; j++)
{
if(s[i] < s[j])
{
ch = s[i];
s[i] = s[j];
s[j] = ch;
}
}
}
}
void not_dict(char s[]) { int i,j; char ch; for(i=0; s[i] != '\0'; i++) { for(j=i+1;s[j] != '\0'; j++) { if(s[i] < s[j]) { ch = s[i]; s[i] = s[j]; s[j] = ch; } } } }
Using above define function 'not_dict()':
void main()
{
char s[20] = "abcxyz";
not_dict(s);
printf("String In Non Alphabatical order is: ");
puts(s);
}
void main() { char s[20] = "abcxyz"; not_dict(s); printf("String In Non Alphabatical order is: "); puts(s); }
Output1
String In Non Alphabetical order is: zyxcba
Using above define function 'not_dict()':
void main()
{
char s[20] = "DCBA";
not_dict(s);
printf("String In Non Alphabatical order is: ");
puts(s);
}
void main() { char s[20] = "DCBA"; not_dict(s); printf("String In Non Alphabatical order is: "); puts(s); }
Output2
String In Non Alphabetical order is: DCBA
Using above define function 'not_dict()':
void main()
{
char s[20] = "LMNO";
not_dict(s);
printf("String In Non Alphabatical order is: ");
puts(s);
}
void main() { char s[20] = "LMNO"; not_dict(s); printf("String In Non Alphabatical order is: "); puts(s); }
Output3
String In Non Alphabetical order is: ONML
0 Comments
Post a Comment