Click here to Skip to main content
15,889,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone!I have to run a program who read a string from the user and print the string reverse, the character with the most frequences and the character with less frequences.So, i have made two functions but i have a problem when i run my code.My program print the string reverse and after this crashes.Can you help me to find my mistake?For example, when my input is hello world my output is dlrow olleh and after that my program crashes and does not appear the result of my other function.

What I have tried:

Here is my code,
C++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void chars(char *s, char *most_seen, char *less_seen)
{
    char *s2=malloc((strlen(s)+1)*sizeof(char)); //create a new string 
    int i,j,count=0,count1=1,count2=0,count3=100;
    //gets(s1);
    strcpy(s2,s); //copy the s to s2 to compare the strings

    //compare the strings to find the character which appear more in the string 
    for(i=0;i<strlen(s);i++)
    {
        for(j=0;j<strlen(s);j++)
        {
            if(s[i]==s2[j])
                count2++;
        }
        if(count2>count1)
        {
            count1=count2;
            *most_seen=s[i];
        }
        count2=0;
    }
     //compare the strings to find the character which appear less in the string 
     for(i=0;i<strlen(s);i++)
    {
        for(j=0;j<strlen(s);j++)
        {
            if(s[i]==s2[j])
            {
                count++;
            }
        }
        if(count<count3)
        {
            count3=count;
            *less_seen=s[i];
        }
        count=0;
    }
}

char *reverse(char *s)
{
   int l, i;
    char *begin_ptr, *end_ptr, ch;

    // Get the length of the string
    l = strlen(s);

    begin_ptr = s;
    end_ptr = s;

    for (i = 0; i < l - 1; i++)
        end_ptr++;
    for (i = 0; i < l / 2; i++) {
        ch = *end_ptr;
        *end_ptr = *begin_ptr;
        *begin_ptr = ch;

         begin_ptr++;
         end_ptr--;
    }
}

int main()
{
    char *s=malloc(1000*sizeof(char));
    char *s2;
    char mostSeen,lessSeen;

    fgets(s,1000,stdin);
    s[strcspn(s,"\n")] = '\0';

    chars(s,&mostSeen, &lessSeen);
    s2 = reverse(s);

    printf("%s\n",s);
    printf("%s\n",s2);
    printf("%c\n%c\n",mostSeen,lessSeen);

    return 0;
}
Posted
Updated 4-Sep-20 22:42pm
v3
Comments
KarstenK 5-Sep-20 4:43am    
did you try to use the debugger with its memory view?

Simple: your reverse method doesn't return a value:
C++
char *reverse(char *s)
{
   int l, i;
    char *begin_ptr, *end_ptr, ch;

    // Get the length of the string
    l = strlen(s);

    begin_ptr = s;
    end_ptr = s;

    for (i = 0; i < l - 1; i++)
        end_ptr++;
    for (i = 0; i < l / 2; i++) {


        ch = *end_ptr;
        *end_ptr = *begin_ptr;
        *begin_ptr = ch;

         begin_ptr++;
         end_ptr--;
    }


    }
You are actually reversing the string inplace - so the output your see: "dlrow olleh" is coming from your first printf, not your second.

A much, much better idea would be to do the reverse into a new buffer and to return that (using malloc as in your other function), but you can just add
C++
return s;
to the end of the function.
 
Share this answer
 
Oh you are right!Thank you very much!!
 
Share this answer
 
Comments
OriginalGriff 5-Sep-20 4:49am    
You're welcome!
Don't post comments like this as a solution - use the "Have a Question or Comment?" button instead. That way, the poster gets an email - just like this one - to let them know you are speaking to them.

Would you like me to delete this for you?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900