returning achanged string..

This is a discussion on returning achanged string.. within the C Programming forums, part of the General Programming Boards category; i got a string which has words in it which are separated by spaces. i need to build a function ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    returning achanged string..

    i got a string which has words in it which are separated by spaces.

    i need to build a function sortbycount(char* str) uses "countletters" function in order to know which word has a unique number of chars
    in a word.
    and returns a string in which the words are sorted by ascending order of words
    regardind their "countletters" value of this word.
    for example:
    "ababa abcd abbbbA dbBdc abbcCc"

    returns
    "abcd abbcCc dbBdc ababa abbbbA"

    i got to find the shortest way to do that.

    my main problem is the sorting parting part.
    if the first word will be 5
    and the next will be 10
    and the next will return 15
    i will need to push the previus words to the second place and put it into the first
    and i can have a million words
    how can i know in advance which word to put where
    here is the countletters code
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct node node;
    struct node{
    	int value;
    	struct node * next;
    };
     int countletters(char *str);
    void main()
    {
    	int g;
    	char  str[18]="aabx bXcb bBxaDAa";
    	g=countletters(str);
    }
    
    
    int countletters(char *str)
    {
      char ch='a';
      char *st;
      int cnt=0;
       
       for(;ch<='z';ch++)
       {
    
          for(st=str;*st!='\0';st++)
    	  {
               if (*st==ch)
    		   {
                  cnt++;
    			  break;
    		   }
    		   if ((*st>='A')&&(*st<='Z'))
    		   {
                  if (ch==(*st+32))
    		      {
                     cnt++;
    			     break;
    		      }
    		   }
    	  }
       }
       return cnt;
    }
    Last edited by transgalactic2; 06-20-2009 at 08:21 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can't know in advance, just put all the values into your linked list, and when you're through doing that, *then* sort the whole list according to the number of letters.

    I'd recommend selection sort.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ohh sorryy the struct has left from the previous question i was doing

    there is no linked list here
    ??

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    its a question from a test.i am looking for the smallest code solution possible.

    i thought to count the gaps and to create an array of string using malloc of (number of gaps+1)
    and then sort the array by the value i get from each string.
    then use bubble sort to sort it
    then to unite them using a loop and sprinf

    it sounds terribly long more time then i would in a test.
    is there any shorter way?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I saw the *next in the struct and thought "it's a linked list". Force of habit, I suppose.

    I would use a struct to hold:

    1) The string of letters
    2) The length of the string of letters in part 1

    The reason being that when you sort, you'll make *lots* of comparisons, and you'll want those comparisons to be based on the length of the string of letters (not the letters themselves, of course). So if you don't save that number, you'll have to recalculate the length of each string, over and over. Very wasteful.

    I like your algorithm, otherwise. As far as time to code it goes, I'd do this - practice writing up that code, several times. Practice it until you can almost write it perfectly, half asleep.

    You'll be surprised at how much your coding time will decrease as you continue to practice. Especially the tricky parts - maybe mallocing a 2D array, or maybe passing the 2D array to the function.

    Practice may not make you perfect, but it will improve your time needed, by a boat load full.

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how to beak the string into substrings.

    i can get the end point of a substring

    how i know the start
    ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String search not returning correctly?
    By Blackroot in forum C++ Programming
    Replies: 5
    Last Post: 08-18-2008, 06:41 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21