the code is:

slen=strlen(str);
    for(count=0;count<=slen;count++)
    {
        flag=0;
        if((int)str[count]!=32)
        {
            for(count2=count+1;count2<=slen;count2++)
            {
                if(str[count]==str[count2])
                {
                    flag=1;
                    break;
                }
            }
            if(flag==0)
            {
                ar[ar_count]=str[count];
                ar_count++;
            }

        }
    }

at line 5 the != operator working fine but when i put if(((int)str[count]>=65 && (int)str[count]<=90) || ((int)str[count]>=97 && (int)str[count]<=122)) it's not working..

if you've some time to spare and want to know more about this program then read the following:
the question is here. the guy is answered the solution there, but since i don't know anything about pointer or file handeling i didn't understand a thing there...:sad:
so here's my program:

#include <stdio.h>
#include <string.h>
int main()
{
    int count=0,count2,ar_count=0,tempch,slen,flag,freq[35],freqcount;
    char str[35],ar[35];
    printf("Enter the string you want to reorganize: ");
    fflush(stdout);
    fgets(str,34,stdin);
    while(str[count]!='\0')     //converting uppercases to lowercase
    {
        tempch=str[count];
        if(tempch>=65 && tempch<=90)
        {
            tempch+=32;
            str[count]=tempch;
        }
        count++;
    }
    /* putting unique characters in ar[] */
    slen=strlen(str);
    for(count=0;count<=slen;count++)
    {
        flag=0;
        if(((int)str[count]>=65 && (int)str[count]<=90) || ((int)str[count]>=97 && (int)str[count]<=122))
        {
            for(count2=count+1;count2<=slen;count2++)
            {
                if(str[count]==str[count2])
                {
                    flag=1;
                    break;
                }
            }
            if(flag==0)
            {
                ar[ar_count]=str[count];
                ar_count++;
        }

        }
    }
    /* unique array order alphabatically */
    while(1)
    {
        flag=0;
        for(count=0;count<=ar_count-3;count++)
        {
            if((int)ar[count]>(int)ar[count+1])
            {
                tempch=ar[count];
                ar[count]=ar[count+1];
                ar[count+1]=tempch;
                flag=1;             //flag=1 if the atleast one char got swapped
            }
        }
        if(flag==0)break;         //all chars are in order already
    }
    /* frequency of the array elements */
    for(count=0;ar[count]!='\0';count++)
    {
        freqcount=0;
        for(count2=0;count2<=slen;count2++)
        {
            if(ar[count]==str[count2])freqcount++;
        }
        freq[count]=freqcount;
    }
    /* printing the output */
    for(count=0;count<=ar[count]!='\0';count++)
    {
        for(count2=1;count2<=freq[count];count2++)
        {
            printf("%c",ar[count]);
        }
    }
    return 0;
}

Now when i'm trying to get rid of the non-alphabet characters in line 25 it's not working.
Any help will be appreciated. And any criticism about my bad practice or logic will be welcomed (actually i myself think i kinda overkilled the process :sweat:)
Thank you all.

i'm having a hard time following your question... what exactly are you trying to do?

one thing you should know that will help you, is you don't need to cast each char as an int just to do a comparison to a constant value. a char type is essentially just an 8-bit integer, so you can directly compare it to some constant numeric value. for instance, check out this snippet that converts all lowercase to uppercase:

char myString[32] = {"This... is 1 Example String!"};
int length,  i;

printf("original string : %s\n", myString);

length = strlen(myString);
for (i = 0; i < length; i++)
   if (myString[i] > 96 && myString[i] < 123)  // if char is lowercase
      myString[i] -= 32;                       // convert to uppercase

printf("modified string : %s\n", myString);

.

if(((int)str[count]>=65 && (int)str[count]<=90) || ((int)str[count]>=97 && (int)str[count]<=122))

it would make more sense like this so that you don't have to think about what the decimal values for 'A' and 'a' are. You are trying to find out if the character is A-Z or a-z, not 65-90.

if( (str[count]>='A' && str[count]<='Z') || (str[count]>='a' && str[count]<='z') )

one thing you should know that will help you, is you don't need to cast each char as an int just to do a comparison to a constant value. a char type is essentially just an 8-bit integer, so you can directly compare it to some constant numeric value. for instance, check out this snippet that converts all lowercase to uppercase:

oh...i didn't know that. thanks for the info.

if(((int)str[count]>=65 && (int)str[count]<=90) || ((int)str[count]>=97 && (int)str[count]<=122))

it would make more sense like this so that you don't have to think about what the decimal values for 'A' and 'a' are. You are trying to find out if the character is A-Z or a-z, not 65-90.

if( (str[count]>='A' && str[count]<='Z') || (str[count]>='a' && str[count]<='z') )

yeah..that's right. But i was wondering if when the value of str[count] is 'B' it will satisfy or not. 'B' will be counted as >= 'A'??


But guys neither of these are working here. Both the time it's blank.

When i'm putting if(str[count]>'96' && str[count]<'123') in line 25...
these build messages are showing up:

G:\my projects\C\word_order_frequency2.c|25|warning: multi-character character constant|
G:\my projects\C\word_order_frequency2.c||In function `int main()':|
G:\my projects\C\word_order_frequency2.c|25|warning: comparison is always false due to limited range of data type|
G:\my projects\C\word_order_frequency2.c|25|warning: multi-character character constant|
G:\my projects\C\word_order_frequency2.c|25|warning: comparison is always true due to limited range of data type|
||=== Build finished: 0 errors, 4 warnings ===|

when putting if(str[count]>='a' && str[count]<='z') absolutely nothing is happening...no error...no warning..like before....

i'm having a hard time following your question... what exactly are you trying to do?

The program is working fine but in line 25 i just trying to add an extra filter, so that only the characters from a-z can pass....

the aim of the program is:

1. Parse a string into characters and arrange these characters in descending order according to their frequency/number of occurrence [all characters are converted to lowercase].
2. If two different characters have the same count of frequency, they must be ordered alphabetically.
3. Words are all case INsensitive and are all made of English alphabet.
4.Print the results in the form of a word.

if you give it 'Engineering' it will give you 'eeeggiinnnr'...
Now i'm trying to do if user give 'Hello world!!', the output will be:
'dehllloorw'
now if i try to get rid of 'space' only using !=32 , it's working fine but filtering a-z is not working...Getting my point?
Please run the program i posted in my earlier post replacing line 25.
thanks for help guys...

>>f(str[count]>'96' && str[count]<'123')

Use your head a little -- In C language you don't put ' around numeric values and you can not compare a single character to a string.

>>f(str[count]>'96' && str[count]<'123')

Use your head a little -- In C language you don't put ' around numeric values and you can not compare a single character to a string.

err...what do you mean?i didn't understand you point...:icon_neutral:
in string str....str[count] is a character right? so can't i check the character like.. ((int)str[count]>=97 && (int)str[count]<=122) ??

jephthah said there is no need to convert every character in a int. one can compare char with it's ASCII value directly.

one thing you should know that will help you, is you don't need to cast each char as an int just to do a comparison to a constant value. a char type is essentially just an 8-bit integer, so you can directly compare it to some constant numeric value

i tried this way... if(str[count]>'96' && str[count]<'123') it works fine normally...but here in this program it's not working.:?:

you're putting single quotes around a numeric value. don't do that.

single quotes frame a single ascii character. double quotes frame an ascii string. if you want to compare a numeric, then just put the numeric without any quotes.

'A' is the character for the letter A. it has a value of \x41 or decimal 65.

'65' is undefined. single quotes are for one and only one character. who knows how this will be interpreted! Maybe it will be interpreted as the first character, '6', which is the ascii character having the value of \x36 or decimal 54.

"65" on the other hand is a string, having the value \x36\x35\x00


Look at an ascii chart. any single ascii character as a 'char' data type is inherently represented as an 8-bit integer value. the 'char' and it's integer value are fundamentally identical to each other.

the ascii char 'A' equals 65 equals the ascii char 'A'. the ascii char 'B' equals 66 equals the ascii char 'B'. the ascii char 'Z' equals 90 equals the ascii char 'Z'

65 <= 66 <= 90 --> TRUE

'A' <= 'B' <= 'Z' --> TRUE


.

thanks for the heads up guys...sorry...i did a mistake when replying the earlier post...
i actually did put if(str[count]>96 && str[count]<123) ...BIG sorry...for my mistake you had to explain all these...but it helped anyway...just learnt many things i didn't know... But still it's not working....
here's the new code:

#include <stdio.h>
#include <string.h>
int main()
{
    int count=0,count2,ar_count=0,tempch,slen,flag,freq[35],freqcount;
    char str[35],ar[35];
    printf("Enter the string you want to reorganize: ");
    fflush(stdout);
    fgets(str,34,stdin);
    while(str[count]!='\0')     //converting uppercases to lowercase
    {
        tempch=str[count];
        if(tempch>=65 && tempch<=90)
        {
            tempch+=32;
            str[count]=tempch;
        }
        count++;
    }
    /* putting unique characters in ar[] */
    slen=strlen(str);
    for(count=0;count<=slen;count++)
    {
        flag=0;
        if(str[count]>96 && str[count]<123)
        {
            for(count2=count+1;count2<=slen;count2++)
            {
                if(str[count]==str[count2])
                {
                    flag=1;
                    break;
                }
            }
            if(flag==0)
            {
                ar[ar_count]=str[count];
                ar_count++;
            }
        }
    }
    /* unique array order alphabatically */
    while(1)
    {
        flag=0;
        for(count=0;count<=ar_count-3;count++)
        {
            if((int)ar[count]>(int)ar[count+1])
            {
                tempch=ar[count];
                ar[count]=ar[count+1];
                ar[count+1]=tempch;
                flag=1;             //flag=1 if the atleast one char got swapped
            }
        }
        if(flag==0)break;         //all chars are in order already
    }
    /* frequency of the array elements */
    for(count=0;ar[count]!='\0';count++)
    {
        freqcount=0;
        for(count2=0;count2<=slen;count2++)
        {
            if(ar[count]==str[count2])freqcount++;
        }
        freq[count]=freqcount;
    }
    /* printing the output */
    for(count=0;count<=ar[count]!='\0';count++)
    {
        for(count2=1;count2<=freq[count];count2++)
        {
            printf("%c",ar[count]);
        }
    }
    return 0;
}

thanks to all...

if(tempch>=65 && tempch<=90) tempch+=32; if(str[count]>96 && str[count]<123) What are these magic numbers?

After all that discussion since last night about using the characters themselves ('A', 'z', ' ') you still post code with magic numbers?

Seems AD and J just wasted a night trying to help.

But still it's not working....
here's the new code:

So still the exact same problem? You fixed nothing with all that from last night? Or is this a new problem?

You know, looking back on your posts you love to tell us "it's not working" but you like to keep what's actually happening a secret. Don't keep secrets! Don't tell us it's not working. We figured that's why you're here. What we care about is what exactly it is doing wrong. Exactly. Not a vague "it's broke". A specific "it does this and this instead of that and that". Please be detailed.

commented: Yes, I may as well have ignored this thread. +27

What are these magic numbers?

After all that discussion since last night about using the characters themselves ('A', 'z', ' ') you still post code with magic numbers?

I'd request you to take a proper look in my replies before shouting at me...

when putting if(str[count]>='a' && str[count]<='z') absolutely nothing is happening...no error...no warning..like before....

since both are having the same result i just post one of them.
here's the new code replacing the magic numbers:

#include <stdio.h>
#include <string.h>
int main()
{
    int count=0,count2,ar_count=0,slen,tempch,flag,freq[35],freqcount;
    char str[35],ar[35];
    printf("Enter the string you want to reorganize: ");
    fflush(stdout);
    fgets(str,34,stdin);
    while(str[count]!='\0')     //converting uppercases to lowercase
    {
        if(str[count]>='A' && str[count]<='Z')
        {
            str[count]+=32;

        }
        count++;
    }
    /* putting unique characters in ar[] */
    slen=strlen(str);
    for(count=0;count<=slen;count++)
    {
        flag=0;
        if(str[count]>='a' && str[count]<='z')
        {
            for(count2=count+1;count2<=slen;count2++)
            {
                if(str[count]==str[count2])
                {
                    flag=1;
                    break;
                }
            }
            if(flag==0)
            {
                ar[ar_count]=str[count];
                ar_count++;
            }
        }
    }
    /* unique array order alphabatically */
    while(1)
    {
        flag=0;
        for(count=0;count<=ar_count-3;count++)
        {
            if((int)ar[count]>(int)ar[count+1])
            {
                tempch=ar[count];
                ar[count]=ar[count+1];
                ar[count+1]=tempch;
                flag=1;             //flag=1 if the atleast one char got swapped
            }
        }
        if(flag==0)break;         //all chars are in order already
    }
    /* frequency of the array elements */
    for(count=0;ar[count]!='\0';count++)
    {
        freqcount=0;
        for(count2=0;count2<=slen;count2++)
        {
            if(ar[count]==str[count2])freqcount++;
        }
        freq[count]=freqcount;
    }
    /* printing the output */
    for(count=0;count<=ar[count]!='\0';count++)
    {
        for(count2=1;count2<=freq[count];count2++)
        {
            printf("%c",ar[count]);
        }
    }
    return 0;
}

except the filter in line 25 it's working fine...when i'm putting that...its' not working....
i'm adding that line so that only a-z can pass through the filter...
but can't figure out why it's not working..<<That's the problem
thanks to all for helping...

i dont understand what you're trying to accomplish. it's not clear to me.

in your first post, you did refer to an old link, but to be honest, i'm not going to search through several pages of some old discussion to try and guess what you may have gotten out of it.

so, i think this is part of your problem here, that some people can get frustrated when problems aren't explained clearly. don't take it personal. i see that you were just trying to get to the point, but remember that we're not thinking about this problem like you are, so you need to back up and attempt to clearly explain the issue in as concise a manner as possible.

Now if i just make a guess about your current issue: it seems like youve got a problem in the second loop starting at line 26, such that you will never save any character if it ever matches another character. i'm not sure if that's what you intend to do.

may be my english isn't that good, that's why these communication gaps are happening...:(
Guys...FORGET EVERYTHING I SAID....Let me describe from the beginning...
The thing i'm trying to achieve:
1. Parse a string into characters and arrange these characters in descending order according to their frequency/number of occurrence [all characters are converted to lowercase].
2. If two different characters have the same count of frequency, they must be ordered alphabetically.
3. Words are all case INsensitive and are all made of English alphabet.
4.Print the results in the form of a word.

^^^That means if the input is Engineering , the out put will be eeeggiinnnr .
my code works properly for this...see here

So if you give input only one word(i.e. Engineering) everthing is ok...But what if the input is a sentence? i.e. "Hello World"
in that case the output will be " dehllloorw"(see the extra space before 'dehllloorw')...since the order is alphabetically...the 'space(s)' will go to the beginning of the output, cause space's ASCII value is lower than the english alphabets. I want to get rid of this 'space'(and other unwanted characters...i only need lowercase english alphabets: a to z)...

In my code i was comparing the characters and putting the unique characters in a separate string.
so i put an if there, so that only a to z characters reach that comparison part...
see the new code here
i put that if in line 24...And run the file...But it was NOT giving output anymore...<<This is the problem

I think my logic is right...Cause instead of checking a to z..i first tried to get rid of the 'space' only...so i did put if(str[count]!=32) ...and it was working fine, the
'spaces' were filtered...no 'space' was coming in output....
But when i put if(str[count]>='a' && str[count]<='z') the code's not giving output...Blank screen...
I tried my best to make you guys understand my problem...Hope this helps...
Thank you all...

Now if i just make a guess about your current issue: it seems like youve got a problem in the second loop starting at line 26, such that you will never save any character if it ever matches another character. i'm not sure if that's what you intend to do.

No J, problem is in line 24 only, from the beginning i'm trying to say this..I don't know when and where i made you guys confused...but there was always only one problem...that's in line 24...the if statement before the loop...

I think I get it now. Thanks for the detailed explanation.

You are making it way too complex. Your first loop is fine, just lowercase all the letters:

while(str[count]!='\0')     //converting uppercases to lowercase
    {
        if(str[count]>='A' && str[count]<='Z')
        {
            str[count]+=32;  // this magic number is OK

        }
        count++;
    }

Your next loop is way too complex for the job your comment is claiming: /* putting unique characters in ar[] */ First thing is, you don't want unique, you want only letter characters.
Make a loop similar to the first one. You need 2 counters, 1st for the array you read, 2nd for the location in the new array where the next character goes. Start them both at 0

count = 0;
ar_count= 0;

Now when you move the character to the new array, you use something like ar[ar_count]=str[count]; like you did, and increment both counters. But if you don't move the character (it's a SPACE or a QUOTE) only increment count. Don't increment ar_count.

This loop including the initialization above should be about 6 code lines long with 1 and only 1 loop. Remember, this new array is NOT a string, it is simply an array of characters.

Now display the new array and see if it worked.

If you type in "Today this sunny day, IS bright!", your new array should display "todaythissunnydayisbright"


To be continued...

thanks for your reply Walt.
Actually what i'm doing here is:

Suppose the input is "Engineering" stored in str[]
>First lowercase all the the uppercases....."engineering"
>Putting unique characters in a different array ar[]....."ering"
>now changing the array elements in order in ar[]..."eginr"..
i did this step because part of the question was: If two different characters have the same count of frequency, they must be ordered alphabetically.
>Now comparing ar[] with str[], for knowing the frequency of every letter character...and store the number in freq[]..
so what i got is:
ar[0]=e ~ freq[0]=3
ar[1]=g ~ freq[1]=2
ar[2]=i ~ freq[2]=2
ar[3]=n ~ freq[3]=3
ar[4]=r ~ freq[4]=1
>printing the characters according to their frequency..."eeeggiinnnr".
i was filtering a-z in a step...in your way i've to use an extra array for eliminating unwanted characters right?
i'll try that and let you know. but i wonder why this isn't working.
thanks for your help. i'll get back asap.

Assuming the code works that's based on my suggestion, now create a frequency array to count each letter -- freq[26]. Now loop through the array and use the character itself as an index into that array: freq[ar[i]-'a']++ ar is the current character
subtract 'a' from it to offset the value to 0.
++ increments that frequency value.

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.