Write a Program That Reads in Three Strings and Sorts Them Lexicographicallycoursehero

  1. #1

    woozy is offline

    Registered User


    Question Sort strings in lexicographical lodge just no built-in string functions immune

    Subsequently many advices, helps, I could somewhen solve this problem myself, happy~~

    All I did was to write a replacement office for strcmp() and then use a bubble sort function to sort input strings in lexicographical guild.

    Much simpler than nasty nested loops :P

    my_strcmp():

    Code:

    int my_strcmp (char *str1, char *str2) {   	int id = 0;  	char *temp1, *temp2;  	temp1 = my_strlwr(str1); 	temp2 = my_strlwr(str2);  	while(!(id = *temp1 - *temp2) && *temp2) 	{ 		++temp1, ++temp2; 	}  	if (id < 0) 	{ 		id = -1; 	} 	else if (id > 0) 	{ 		id = 1; 	}  	return id; }
    In fact, I got the realization from the source lawmaking algorithm. The important part is that I understand how this algorithm works now.

    Below is the original problem:

    Hi,

    I desire to write a function to perform lexicographical sorting using a simple bubble sort technique. So far I've got the bones lawmaking running, but something is not right. I tin can tell the algorithm is wrong but cannot fix it, please assist.

    Hither is my function:

    Code:

    int bubble_sort(char **words, int num_word) { 	int ten, y, z; 	char *temp;  	for (x = 0; 10 < num_word; x++) 	{ 		for (y = 0; y < (num_word-one); y++) 		{ 			for (z = 0; z < (WORD_SIZE+i); z++) 			{ 				if (words[y][z] < words[y+1][z]) 				{ 					temp = words[y+1]; 					words[y+i] = words[y]; 					words[y] = temp; 					break; 				} 				else 				{ 					go along; 				} 			} 		} 	}  	return 0; }

    I've tried not to use the 3rd inner loop, but and then information technology will only perform the first alphabetic character comparison...And yes, let me stress that strcmp or strncmp should not be used...

    Thank you in advance.

    Terminal edited by woozy; 10-21-2007 at 02:45 PM.


  2. #two

    Salem is offline

    and the lid of int overfl Salem's Avatar


    Write your own drib-in replacement for strcmp() so yous can simplify your lawmaking.

    Or use strcmp() anyway until the sort is working, and so develop your own strcmp() replacement.

    Nobody said you couldn't utilize the standard library to test with


  3. #iii

    woozy is offline

    Registered User


    Really the purpose of what I'1000 doing hither is to empathize the algorithm of strcmp.

    So yes, I'd rather not use information technology and keep on trying (and keep on trying to ask likewise ;p)


  4. #iv

    laserlight is offline

    C++ Witch laserlight's Avatar


    Actually the purpose of what I'yard doing here is to empathize the algorithm of strcmp.

    In that example, do not complicate information technology by integrating it into a sorting algorithm. Just write it as a function, then test with a few cases.

    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)

    I become perhaps two dozen requests for assist with some sort of programming or blueprint problem every day. About accept more sense than to ship me hundreds of lines of code. If they do, I ask them to detect the smallest instance that exhibits the trouble and send me that. More often than not, they then find the error themselves. "Finding the smallest program that demonstrates the fault" is a powerful debugging tool.

    Look up a C++ Reference and learn How To Ask Questions The Smart Mode


  5. #5

    woozy is offline

    Registered User


    Then the other purpose fails - which is to sort these give-and-take strings into lexicographical order.

    Only anyways, I agree with both of ya, I'll endeavor to write a replacement for strcmp get-go, then subsequently on drib it into my sorting loop.


  6. #vi

    laserlight is offline

    C++ Witch laserlight's Avatar


    Quote Originally Posted by woozy View Post

    Then the other purpose fails - which is to sort these discussion strings into lexicographical guild.

    But anyways, I concur with both of ya, I'll effort to write a replacement for strcmp first, then later on driblet it into my sorting loop.

    That'due south correct. It is easier to implement 1 at a time and know that ane works before trying the next. Otherwise you will be left wondering whether the bug (or bugs) lies in your cord comparison, your sorting, or both.

    Quote Originally Posted by Bjarne Stroustrup (2000-x-xiv)

    I become maybe ii dozen requests for assistance with some sort of programming or design trouble every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest case that exhibits the problem and send me that. Mostly, they and so discover the error themselves. "Finding the smallest plan that demonstrates the mistake" is a powerful debugging tool.

    Look up a C++ Reference and larn How To Ask Questions The Smart Way


  7. #7

    woozy is offline

    Registered User


    Now I've done a replacement for strcmp(), and besides I found the source code of strcmp() in the standard lib...

    I wonder which ane is more efficient

    mine:

    Lawmaking:

    int my_strcmp(char *str1, char *str2)   {   	char* p1; 	char* p2; 	 	p1 = str1; 	p2 = str2;  	while (*p1 && *p2) 	{ 		if (*p1 == *p2) 		{ 			p1++; 			p2++; 		} 		else 		{ 			return (*p1 - *p2); 		} 	} 	 	return (*p1 - *p2); }
    strcmp() source rewritten:

    Code:

    int my_strcmp (char *str1, char *str2) {   	int id = 0;    	while(!(id = *str1 - *str2) && *str2) 	{ 		++str1,   ++str2; 	}  	if (id < 0) 	{ 		id   =   -one; 	} 	else if (id > 0) 	{ 		id = 1; 	}  	return id; }


  8. #8

    whiteflags is offline

    Lurking whiteflags's Avatar


    Information technology probably doesn't matter. Yous tin't compare two character strings for equality without taking into account that the but identify ii strings might differ is at the end. Therefore, even aggressively optimized comparisons are linear time.


  9. #9

    woozy is offline

    Registered User


    Hi citizen,

    Thank y'all for your advice. I'thou not yet trying to optimize anything, but to implement some algorithm.


standardhisper.blogspot.com

Source: https://cboard.cprogramming.com/c-programming/94841-sort-strings-lexicographical-order-but-no-built-string-functions-allowed.html

0 Response to "Write a Program That Reads in Three Strings and Sorts Them Lexicographicallycoursehero"

Publicar un comentario

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel