Hi everyone. I'm new to the forums, as well a C programming as well, so bare with me for any noob mistakes. I have a homework assignment I am working on now, and am just completely stumped. The assignment calls for the following

The user is to input 10 words. It then takes those words, changes the lower and upper case around (were not allowed using the toupper() or tolower () functions), as well as spelling those words backwards. Heres a brief example

Enter Word 1: tHe
Enter Word 2: seCOND
Enter Word 3: wOrD
... etc etc up until the 10th word

The output would then be the following:

EhTThE
dnocESSEcond
dRoWWord

Everyone catch my drift. Now from this, I must take the unsorted output, and bubble sort it in ascending order. Here is the code I have come up with. Any help is greatly appreciated

#include <stdio.h>
 #include <string.h>
 
 int main(void)
 {
 char Word[10][19];
 int i, j, k, l, z;
 int value;
 char swap[19];
 char ch;

 
 for(z=0 ; z<10 ; z++)
 {
 if (z == 0)
   printf("Enter the 1st word: ");
 else if (z == 1)
   printf("Enter the 2nd word: ");
 else if (z == 2)
   printf("Enter the 3rd word: ");
 else
   printf("Enter the %dth word: ",z+1);
 i=0;
while(( ch = fgetc(stdin)) != '\n')
 {
		i=i+1;
		Word[z][i]= ch;
		
 }
	l=10;
 for(k=i ; k>=1; k--)
	{
	Word[z][l]=Word[z][k];
	l=l++;
	}
	Word[z][19]=i;
}
printf("\n");
printf("Unsorted Input: \n");
printf("\n");
for (z=0 ; z<10 ; z++)
{
 for(j=10 ; j<=9+Word[z][19]; j++)
	{
	value = Word[z][j];
	Word[z][j] = '\0';
	if (value >= 97 && value<=122)
	{ value = value - 32; }
	else if (value >=67 && value<=90)
	{ value = value + 32; }
	printf("%c", value);
	}
 for(j=1; j<=Word[z][19]; j++)
	{
	value = Word[z][j];
	if (value >= 97 && value<=122)
	{ value = value - 32; }
	else if (value >=67 && value<=90)
	{ value = value + 32; }
	printf("%c", value);
	}	
putchar('\n');
	}
printf("\n");
printf("Sorted Input: \n");
printf("\n");

}

Yes, Im just unclear on how to perform the bubble sort to sort the list of words. Any info would be great.

Use strcmp() to compare the two strings. if (strcmp( words[j], words[k] ) < 0) { swap j and k } The strcmp() function is nice because the operator you use (less-than, equal-to, or greater-than) is exactly the same as it would be if you could put it between the strings like:

if (words[j] < words[k]) ...

Hope this helps.

Ok, well I took a look at the strcpy and strcmp functions, and tried to my ability to put a piece of code together to sort, and then print the sorted list, but for the life of me cannot get it to work.

printf("\n");
printf("Sorted Input {TESTER}: \n");
printf("\n");

for(z=0; z<j; z++)
printf("%c", Word[z][j]);

for(z=9; z>0; z--) {       
  for(j=0; j<z; j++) {     
    if (strcmp(Word[j],Word[j+1])>0) { 
      strcpy(swap,Word[j]);
      strcpy(Word[j],Word[j+1]);
      strcpy(Word[j+1],swap);
    }
  }
}
}

Any suggestions? Im also not to sure what variables I am printing after it is sorted, this kind of confuses me.

Well, so far I have assumed that Word is an array of char pointers. char *Word[ 10 ]; If that is correct, then to swap you need something like:

char *swap;
...
// swapping part:
  swap = Word[j];
  Word[j] = Word[j+1];
  Word[j+1] = swap;

However, if your Word is a 2D array: char Word[ 10 ][ 100 ]; char swap[ 100 ]; then your swap should work fine.

Your bubble sort looks great.

To print the strings, use one of the following: puts( Word[z] ); printf( "%s\n", Word[z] ); Hope this helps.

Hmmm... for some reason, I still cannot get it. When I compile, this is what I'm gettin:

Error E2342 new4.c 69: Type mismatch in parameter '__s' (wanted 'const signed char *', got 'int') in function main
Warning W8070 new4.c 81: Function should return a value in function main
*** 1 errors in Compile ***

Maybe this is due to the incorrect variable in my for loops or something, I'm not quite sure. I think I'm almost getting this though

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.