returning a changed string function..

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

returning a changed string function..

Postby transgalactic » Sat Jun 20, 2009 7:54 am

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: Select all
#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;
}
transgalactic
 
Posts: 274
Joined: Wed Oct 08, 2008 8:25 am

Re: returning a changed string function..

Postby Alvaro » Sat Jun 20, 2009 9:25 am

Separate the strings and put them in an array. Sort the array. Reconstruct the string.

EDIT: You can count the letters in a single pass, if you keep 26 counters.

You have leftover code in there from something like a linked list... Try to be more tidy. :)

EDIT: I am assuming you are trying to write this in C. In C++ it would be much easier. In Perl it's even easier.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Re: returning a changed string function..

Postby Kison » Sat Jun 20, 2009 10:14 am

Have you made an attempt on the sorting function? If you could post what you have so far, perhaps someone can point you in the right direction.

Also, as Alvaro said, you can perform the counting in one pass!

Code: Select all
#define IS_SET(bv,v) ( (bv) & (1<<v) )
#define SET_BIT(bv,v) ( (bv) |= (1<<v) )

int countletters(char *str)
{
   char *st;
   int cnt=0, lettersUsed=0;
   
   for(st=str;*st!='\0';st++)
   {
      //If this is a letter and it has not been counted before...
      if ( isalpha( (*st) ) && !IS_SET(lettersUsed, tolower( (*st) ) - 'a' ) )
      {
         cnt++;//Increment
         SET_BIT(lettersUsed, tolower( (*st) ) - 'a');//Indicate that this letter has been counted.
      }
   }
   return cnt;
}
User avatar
Kison
 
Posts: 142
Joined: Mon Apr 05, 2004 4:01 pm
Location: Maryland, USA

Re: returning a changed string function..

Postby transgalactic » Sat Jun 20, 2009 12:57 pm

Alvaro wrote:Separate the strings and put them in an array. Sort the array. Reconstruct the string.

EDIT: You can count the letters in a single pass, if you keep 26 counters.

You have leftover code in there from something like a linked list... Try to be more tidy. :)

EDIT: I am assuming you are trying to write this in C. In C++ it would be much easier. In Perl it's even easier.

yes i forgot to delete the struct.
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?
transgalactic
 
Posts: 274
Joined: Wed Oct 08, 2008 8:25 am


Return to Homeworks

Who is online

Users browsing this forum: No registered users and 3 guests