-1

I need help solving this problem, if anyone had a similar problem it would help me a lot.

The task is: Enter string, print and sort only letter from string.

I don't know why program breaks when I want to sort charcter in string, also if I try anything with the variable r in the main, for example to print its length the program will break.

Note, the program needs to be done with the functions listed.

Code is:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 200
#define KV(x) (x)*(x)



char* letter(const char *);
void sort(char *s);
int lenght(char *);
int main()
{
   char s[100];
   char *r;

   printf("Enter string:");
   gets(s);

   r=letter(s);
   sort(r);
   printf("%c",r);
   free(r);
   return 0;
}

char* letter(const char *s)
{
   int i;
   int j=0;
   char *r;
   r=(char*)calloc(lenght(s)+1,sizeof(char));
   for(i=0;s[i]!=0;i++)
   {
      if(s[i]>='a' && s[i]<='z' || s[i]>='A' && s[i]<='Z')
      {
        r[j]=s[i];
        printf("%c",r[j]);
        j++;
      }

   }
}

void sort(char *s)
{
    for(int i=0;i<lenght(s)-1;i++)
    {
        for(int j=i+1;j<lenght(s);j++)
        {
            if(s[i]<s[j])
            {
               char pom=s[i];
               s[i]=s[j];
               s[j]=pom;
            }
        }
    }
 }
int lenght(char *s)
{
  int d;
  for(d=0;s[d]!=0;d++);
  return d;

}

Thanks in advance !

  • 1
    `"I don't know why program breaks when I want to sort charcter in string"` -- Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Jan 22 '22 at 13:32
  • 1
    `r` in your `main` is assigned the return value of function `letter`, but `letter` is missing its `return` statement. If your compiler warned you about this, you should have paid attention to the warning; if not, turn on warnings---they tell you about legal but questionable things in code like this. On most compilers, it's done by adding a `-Wall` flag. – Perette Jan 22 '22 at 16:17
  • 1
    Does this answer your question? [Why does flowing off the end of a non-void function without returning a value not produce a compiler error?](https://stackoverflow.com/questions/1610030/why-does-flowing-off-the-end-of-a-non-void-function-without-returning-a-value-no) – Adrian Mole Feb 01 '22 at 12:16

1 Answers1

0

Try like this:

#include <stdio.h>

int lenght(char *s);
char* letter(char *s);
char* sort(char *s);

int main()
{
    char s[100]; // mix letter and others
    char l[100]; // letter only
    char *p;     // pointer

    printf("Enter string: ");
    gets(s);

    // filter letter
    p = letter(s);
    printf("\nletter: %s\n", p);

    // save letter only to l arrays
    memcpy(l, p, lenght(p));

    // sort letter
    p = sort(l);
    printf("\nsort: %s\n", p);

    return 0;
}

int lenght(char *s)
{
    int d;
    for(d=0; s[d]!=0; d++);
    return d;

}

char* letter(char *s)
{
    int i;
    int j = 0;
    char *r;
    r=(char*)calloc(lenght(s)+1,sizeof(char));
    for(i=0; s[i]!=0; i++)
    {
        if(s[i]>='a' && s[i]<='z' || s[i]>='A' && s[i]<='Z')
        {
            r[j]=s[i];
            //printf("%c",r[j]);
            j++;
        }
    }
    return r;
}

char* sort(char *s)
{
    for(int i=0; i<lenght(s)-1; i++)
    {
        for(int j=i+1; j<lenght(s); j++)
        {
            if(s[i] > s[j])
            {
                char pom = s[i];
                s[i] = s[j];
                s[j] = pom;
            }
        }
    }
    return s;
}

output:

enter image description here

Ihdina
  • 950
  • 6
  • 21