Practice Program
Solution:
#include<stdio.h>
#include<string.h>
void main()
{
char s[3][10];
char s1[20];
int i,j;
for(i=0;i<3;i++)
{
printf("Enter string: ");
gets(s[i]);
}
printf("Strings are: ");
for(i=0;i<3;i++)
{
puts(s[i]);
}
for(i=0;i<3;i++)
{
for(j=i+1;j<3;j++){
if((strcmp(s[i],s[j])) == 1)//comparing the strings if strcmp return 1 then,
{ //we are swapping the both string
strcpy(s1,s[i]);
strcpy(s[i],s[j]);
strcpy(s[j],s1);
}
}
}
printf("String in dictionary order are: ");
for(i=0;i<3;i++)
{
puts(s[i]);
}
}
#include<stdio.h> #include<string.h> void main() { char s[3][10]; char s1[20]; int i,j; for(i=0;i<3;i++) { printf("Enter string: "); gets(s[i]); } printf("Strings are: "); for(i=0;i<3;i++) { puts(s[i]); } for(i=0;i<3;i++) { for(j=i+1;j<3;j++){ if((strcmp(s[i],s[j])) == 1)//comparing the strings if strcmp return 1 then, { //we are swapping the both string strcpy(s1,s[i]); strcpy(s[i],s[j]); strcpy(s[j],s1); } } } printf("String in dictionary order are: "); for(i=0;i<3;i++) { puts(s[i]); } }
Output1
Enter the string: Testing
Enter string: programming
Enter string: Analysis
Strings are: Testing programming Analysis
String in dictionary order are: Analysis programming Testing
Enter string: programming
Enter string: Analysis
Strings are: Testing programming Analysis
String in dictionary order are: Analysis programming Testing
Output2
Enter the string: Zack
Enter string: Ryan
Enter string: Annie
Strings are: Zack Ryan Annie
String in dictionary order are: Annie Ryan Zack
Enter string: Ryan
Enter string: Annie
Strings are: Zack Ryan Annie
String in dictionary order are: Annie Ryan Zack
Output3
Enter the string: Maths
Enter string: Science
Enter string: English
Strings are: Maths Science English
String in dictionary order are: English Maths Science
Enter string: Science
Enter string: English
Strings are: Maths Science English
String in dictionary order are: English Maths Science
defining a function 'to_dictionary' which will arrange strings in dictionary order
void to_dictionary(char s[3][10])
{ char s1[20];
int i,j;
for(i=0;i<3;i++)
{
for(j=i+1;j<3;j++){
if((strcmp(s[i],s[j])) == 1)
{
strcpy(s1,s[i]);
strcpy(s[i],s[j]);
strcpy(s[j],s1);
}
}
}
}
void to_dictionary(char s[3][10]) { char s1[20]; int i,j; for(i=0;i<3;i++) { for(j=i+1;j<3;j++){ if((strcmp(s[i],s[j])) == 1) { strcpy(s1,s[i]); strcpy(s[i],s[j]); strcpy(s[j],s1); } } } }
Using above define function 'to_dictionary()':
void main()
{
char s[3][10] = {"Tester","programmer","Analysis"};
to_dictionary(s);
printf("String in dictionary order are: ");
for(int i=0;i<3;i++)
{
puts(s[i]);
}
}
void main() { char s[3][10] = {"Tester","programmer","Analysis"}; to_dictionary(s); printf("String in dictionary order are: "); for(int i=0;i<3;i++) { puts(s[i]); } }
Output1
String in dictionary order are: Analysis programming Testing
Using above define function 'to_dictionary()':
void main()
{
char s[3][10] = {"ziva","laptop","computer"};
to_dictionary(s);
printf("String in dictionary order are: ");
for(int i=0;i<3;i++)
{
puts(s[i]);
}
}
void main() { char s[3][10] = {"ziva","laptop","computer"}; to_dictionary(s); printf("String in dictionary order are: "); for(int i=0;i<3;i++) { puts(s[i]); } }
Output2
String in dictionary order are: Annie Ryan Zack
Using above define function 'to_dictionary()':
void main()
{
char s[3][10] = {"ziva","laptop","computer"};
to_dictionary(s);
printf("String in dictionary order are: ");
for(int i=0;i<3;i++)
{
puts(s[i]);
}
}
void main() { char s[3][10] = {"ziva","laptop","computer"}; to_dictionary(s); printf("String in dictionary order are: "); for(int i=0;i<3;i++) { puts(s[i]); } }
Output3
String in dictionary order are: English Maths Science
0 Comments
Post a Comment