Modify and Sort Words in C Program: Help with C Programming

In summary: Main()#include <stdio.h>#include <stdlib.h>#include <string.h>char...toupper(char);char...tolower(char);int main(){char words[10][19];char c;int i, j;int k;char swap[10][19];while (j != 10){for (i = 0; i < 10; i++){c = toupper(words[i]);words[i][j] = tolower(words[i][j]);if
  • #1
liukinhei
2
0
Hello,
I am writing a c program to Prompts the user (altogether 10 times) to enter a word not longer than 9 characters: for this part, it doesn't seems working. it didnt return back to the loop to re-entry a correct word(less than 9 characters) " And it needs the modification of each word by changing lower case to upper and then the reverse of the word is concatenated with the word- i can only print out the word (my input : chester, output CHESTER). it can't show like (my input : Chester , output RETSEHccHESTER) , show them in unsorted way and sorted way.
After that, the program terminates. I have made many attempts at writing this program and this is where i am at right now and it still doesn't work. Thanks in advance for the input.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char toupper(char);
char tolower(char);

int main(void)
{
  #include <stdio.h>
#include <stdlib.h>
#include <string.h>

char toupper(char);
char tolower(char);

int main(void)
{
   char words[10][19];
   char c;
   int i, j;
   int k;
   char swap[10][19];

while ( j != 10)
{
   for ( i = 0; i < 10; i++ )
   {

      printf("enter the character \n");
      fflush(stdout);

      for ( j = 0; j < 10; j++ )
      {

         words[i][j] =fgetc(stdin);
         if ( words[i][j] == '\n' )
         {
            words[i][j] = '\0';
            break;
         }
       }
     }
      printf(" you have entered more than 9 characters! \n");  }

   for ( i = 0; i < 10; i++ )
   {
      for ( j = 0; j < 10; j++ )
      {
         k = (int)(words[i][j]);
         if ( k >= 64 && k <= 91 )
         {
            words[i][j]= tolower(words[i][j] );
         }

         else if ( k >= 96 && k<= 123 )
         {
            words[i][j]= toupper(words[i][j] );
         }
      }
   }

   printf("unsort \n");

   for ( i = 0; i < 10; i++ )
   {
      printf("%s \n ", words[i]);
      fflush(stdout);
   }

   printf("Sorted: \n");  
   for ( i=9; i>0; i-- )
   {
      for ( j=0; j<i; j++ )
      {
         if ( strcmp(words[j],words[j+1])>0 )
         {
            strcpy(swap[j],words[j]);
            strcpy(words[j],words[j+1]);
            strcpy(words[j+1],swap[j]);
         }
      }
      printf("%s \n", words[i]);
   }

   return 0;
}


char toupper(char let)
{
   int d;
   d = (int)let;
   let = (char)d-32;
   return let;
}

char tolower(char let)
{
   int d;
   d = (int)let;
   let = (char)d+32;
   return let;
}
 
Last edited:
Technology news on Phys.org
  • #2
liukinhei said:
this is where i am at right now and it still doesn't work.

Can you please be more specific than "doesn't work?" Do you get compiler error messages? Does the program compile, but crash when you try to run it? Does it produce the wrong output? (If so, please give a sample input, the output you expect, and the output you actually get.) Does it make your monitor flash on and off? :smile:
 
  • #3
Another suggestion. Us C/C++ programmers cannot read unindented code.

Please use [ c o d e ] [ / c o d e ] tags (I wrote them with spaces between each character, remove the spaces.) We are helping you, but you have to help us old f*rts.
 
  • #4
Hi Ho!

Let's do it bit-by-bit:

Code:
int main(void)
{
  #include <stdio.h>
#include <stdlib.h>
#include <string.h>

char toupper(char);
char tolower(char);

Duplicated function declarations. Here is your zeroth bug.

Code:
int i, j;
.
.
.
while ( j != 10)

'j' is not initialized. Here you have your first bug.

Code:
while ( j != 10)
{

Unclosed 'while' block in which 'j' is not incremented properly. Here you have your second bug.

I think I must stop here till you provide a better code.


Eus
 
  • #5
Also you could use _gets(), a getline function that goes back to dos (int 21h, function 0ah).

Code:
#define LINESZ 10
static char acBuffer=[LINESZ+2];

int main()
{
int iNumChar;
...

    acBuffer[0] = LINESZ;   // initialize # of characters in buffer
    _cgets(acBuffer);       // read up to LINESZ characters into acBuffer[2...]
    iNumChar = (int)(acBuffer[1]); // get number of characters input
    // ... acBuffer[2]  == 1st character of user input

...
}
 
  • #6
Jeff, showed you how to apply the techniques. You, should code it using a descriptive style associated with your particular solution. I added one storage byte, and tacked on a null byte making the buffer, c-compatible, when it does a name change to "word". Pack this code into a function and you have go.

Example using _gets(), a getline function that goes back to dos (int 21h, function 0ah).

Code:
#define WORDsize 10
static char inWORD [WORDsize+2+1];
static char *word = &inWORD [2]
int main()
{
int charactersIN ;
...

    inWORD[0] = WORDsize   // initialize # of characters for read buffer
    _cgets(inWORD);       // read up to WORDsize  characters into buffer/inWORD[2...]
    charactersIN = (int)(inWORD [1]); // get number of characters input/entered
    inWORD [charactersIN+2] = '\n' ;  // termniate sequence entered with a null byte.
    // ...  inWORD [2]  == 1st character of user input
    // ..
    // ...   word is now a c-string, strlen(word) will return charactersIN
    // ...   and it can be used as: firstCHARACTER = word[0] ;

...
}
 
Last edited:
  • #7
thanks a lot !
it really help!
 
  • #8
inWORD [charactersIN+2] = '\n';
For a C string, you would want

inWORD [charactersIN+2] = 0;
 
  • #9
Hi Ho!

I think the use of _gets () is not portable.
I have revised the code to make it clearer and more portable.
Just give it a try.

Code:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv, char **envp)
{
    char words [10][19] = {0};
    char swap [19] = {0};
    int i = 0;
    int j = 0;
    int k = 0;
    int word_len = 0;

    for (i = 0; i < 10; i++)
    {
        printf ("Enter at most nine characters [#%d]: ", i + 1);

        for (j = 0; j < 10; j++)
        {
            words [i][j] = fgetc (stdin);
            if (words [i][j] == '\n')
            {
                words [i][j] = '\0';
                break;
            }
        }

        if (j == 10)
        {
            printf ("You have entered more than nine characters! Please repeat!\n\n");
            i--;
            while (fgetc (stdin) != '\n');
        }
    }

    for (i = 0; i < 10; i++)
    {
        for (j = 0; j < 10; j++)
        {
            if (isupper (words [i][j]))
            {
                words [i][j] = tolower (words[i][j]);
            }
            else if (islower (words [i][j]))
            {
                words [i][j] = toupper (words[i][j]);
            }
        }
    }

    for (i = 0; i < 10; i++)
    {
        word_len = strlen (words [i]);
        words [i][word_len * 2] = '\0';
        for (j = 0; j < word_len; j++)
        {
            words [i][j + word_len] = words [i][j];
        }
        for (j = 0, k = word_len * 2 - 1; j < k; j++, k--)
        {
            words [i][j] = words [i][k];
        }
    }

    printf("\nUnsorted:\n");

    for (i = 0; i < 10; i++)
    {
        printf ("%s\n", words[i]);
    }

    printf("\nSorted (Ascending):\n");

    for (i = 0; i < 9; i++)
    {
        for (j = 9; j > i ; j--)
        {
            if (strcmp (words [j], words [j - 1]) < 0)
            {
                strcpy (swap, words [j]);
                strcpy (words [j], words [j - 1]);
                strcpy (words [j - 1], swap);
            }
        }
        printf ("%s\n", words [i]);
    }
    printf ("%s\n", words [i]);

    return 0;
}


Eus
 

Related to Modify and Sort Words in C Program: Help with C Programming

1. What is C programming?

C programming is a high-level programming language that was developed in the 1970s. It is widely used for developing operating systems, games, and other software programs. C is known for its efficiency and flexibility, making it a popular choice for programming complex applications.

2. Why do I need help with C programming?

C programming can be challenging for beginners, as it requires a solid understanding of programming concepts and syntax. It is also a low-level language, meaning that it requires more detailed coding compared to other languages. Seeking help can help you learn the language more effectively and avoid common mistakes.

3. What are some common difficulties in C programming?

Some common difficulties in C programming include memory management, pointers, and understanding the syntax. Memory management refers to the allocation and deallocation of memory in a program, which can be a complex task. Pointers, which are variables that store memory addresses, can also be difficult to grasp for beginners. Additionally, the syntax of C can be strict and unforgiving, making it important to pay close attention to detail.

4. How can I improve my skills in C programming?

The best way to improve your skills in C programming is through practice and seeking help from experienced programmers. It is also helpful to read and study code written by others, as well as to participate in online communities and forums where you can ask for advice and learn from others.

5. Are there any resources available for learning C programming?

Yes, there are many resources available for learning C programming, including online tutorials, textbooks, and video courses. It is also helpful to practice on your own by writing small programs and solving coding challenges. Additionally, many universities and community colleges offer courses in C programming for those looking for a more structured learning experience.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
4
Views
750
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
3
Replies
73
Views
4K
  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
Back
Top