Thread: Setting all words in a string in order alphabetically

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    1

    Setting all words in a string in order alphabetically

    I have a string called typedString in C and I need to order the words alphabetically. This code gives me an error of passing argument 1 of ‘strcmp’ makes pointer from integer without a cast expected ‘const char *’ but argument is of type ‘int’. warning: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast. How can i fix this it?

    Code:
    char typedString[300] = " this, is i and a test file, i repeat test file.";
    int len = strlen(typedString);
    char newString[len];
    //int i;
    for (i = 0; i < len; i++) {
      newString[i] = '\0';
    }
    
    int newIndex = 0;
    for (i = 0; i < len; i++) {
      if (typedString[i] >= 'a' && typedString[i] <= 'z' || typedString[i] >= 'A'
          && typedString[i] <= 'Z' || typedString[i] == ' ') {
        newString[newIndex] = typedString[i];
        newIndex++;
      }
    }
    
    char stringList[300][20];
    char *pch;
    pch = strtok(newString, " ,.-");
    int totalWord = 0;
    while (pch != NULL) {
      strcpy(stringList[totalWord], pch);
      pch = strtok(NULL, " ,.");
      totalWord++;
    }
    
    int j;
    char test1[20];
    char test2[20];
    for (i = 0; i < totalWord; i++) {
      strcpy(test1, stringList[i]);
      for (j = i + 1; j < totalWord; j++) {
        strcpy(test2, stringList[j]);
        if (strcmp(strlwr(test1), strlwr(test2)) > 0) {
          char temp[20];
          strcpy(temp, test1);
          strcpy(test1, test2);
          strcpy(test2, temp);
          strcpy(temp, stringList[i]);
          strcpy(stringList[i], stringList[j]);
          strcpy(stringList[j], temp);
        }
      }
    }
    
    printf("\n\nSorted string is\n");
    for (i = 0; i < totalWord; i++) {
      printf("%s ", stringList[i]);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,607
    So what does strlwr() actually return then?


    Your attempt at making newString is broken twice if every character passes the test at line 11.
    1. It's too short by 1 character.
    2. The resulting string has no \0 at the end.


    > pch = strtok(newString, " ,.-");
    > pch = strtok(NULL, " ,.");
    Why do you have different token sets when finding words?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting string alphabetically
    By ofirka in forum C Programming
    Replies: 1
    Last Post: 06-12-2018, 02:43 PM
  2. Replies: 5
    Last Post: 12-19-2016, 05:50 AM
  3. Final project- Sorting words alphabetically
    By Alvin Chang in forum C Programming
    Replies: 9
    Last Post: 12-11-2014, 06:00 PM
  4. Replies: 3
    Last Post: 12-10-2012, 12:24 AM
  5. Sort A String Alphabetically.
    By wantsree in forum C Programming
    Replies: 2
    Last Post: 02-05-2011, 02:14 AM

Tags for this Thread