Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6692213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:50:31+00:00 2026-05-26T05:50:31+00:00

#include<stdio.h> #include<stdlib.h> #define n ((sizeof(char)) * 100 ) int stringlength(char * str) { int

  • 0
#include<stdio.h>
#include<stdlib.h>

#define n ((sizeof(char)) * 100 )

int stringlength(char * str)
{
    int count=0;
    while(*str)
    {  
        if(*str == '\n')
        {
            *str=0;
        }
        else
            count++, str++;
    }
    return count;
}


int palin1(char *str, int k)
{
    char * pend = str + k - 1;
    if(*pend != *str)
        return 0;
    else
        palin1(str+1, k-1);
       return 1;
}    

int palin(char *str)
{
    int length = stringlength(str), f=0;
    char *pend = str + length - 1;
    while(str <= pend)
    {
        if(*str == *pend) f=1;
        else
            return (f = 0);
        str++, pend--;
    }
    return 1;
}

main()
{
    char * ps = (char *)malloc(n);
    int flag;
    if(ps == NULL) printf("Malloc Fail\n");
    else
    {
        printf("Malloc Succeeded, you have memory of %d bytes\n", n);
        printf("This program checks if String is Palindrome or not\n\
        \nEnter your String: ");
        fgets(ps, 100, stdin);
        printf("You entered: %s of length %d", ps, stringlength(ps));
        int i = 0;
        printf("\n\nEnter:\n1.Using iteration\n2.Using Recursion ");
        scanf("%d", &i);
        switch(i)
        {
            case 1:
                flag=palin(ps);
                break;
            case 2:
                flag=palin1(ps,stringlength(ps));
                break;
            default:
                printf("Invalid input");
        }

        if(flag) printf("\nYou entered a Palindrome");
        else  printf("\nNot a Palindrome");
    }
    free (ps);
    return 0;
}

Why does the above program http://www.ideone.com/qpGxi does not give any output on putting the input:

mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm

I know fgets(ps,100,stdin) will take only 100 characters and not more than that, but why does the program halt execution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:50:31+00:00Added an answer on May 26, 2026 at 5:50 am

    You should check for fgets failure, as recommended by the fgets spec.

    if ( fgets(ps,100,stdin) == NULL ) {
        printf("Input failed.");
        //check for 'feof' or 'ferror' here
        return -1;
    }
    printf("You entered: %s of length %d",ps,stringlength(ps));
    

    I don’t see why fgets would be failing, but you would get an uninitialized character buffer back, which would crash printf.

    EDIT: You should really pay attention to your compiler warnings, too.

    prog.c:49: warning: return type defaults to ‘int’
    prog.c: In function ‘main’:
    prog.c:59: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result
    prog.c:63: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
    prog.c: In function ‘palin’:
    prog.c:46: warning: control reaches end of non-void function
    prog.c: In function ‘main’:
    prog.c:52: warning: ‘flag’ may be used uninitialized in this function
    

    You can see that even your compiler recommends checking fgets for null. Also, flag should be set to 0 in the default case, otherwise you will get undefined behavior if the user enters something other than 1 or 2.

    EDIT 2: Oh for Christ’s sake! your program works fine! You forgot to check “run program” in Ideone!!!

    http://www.ideone.com/7ecZd

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
I have the following code: #include <stdlib.h> #include <stdio.h> #include <pthread.h> #define NUM_THREADS 100
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
#include <stdafx.h> #include <stdio.h> #include <conio.h> #include<stdlib.h> #define My_Sizeof(type) ((char*)((type*)0 +1) - (char*)(type*)0) void
#include <stdio.h> #include <stdlib.h> #include <time.h> void initDeck (int deck[]); void showDeck (int deck[]);
#include<stdio.h> #include<signal.h> #include<stdlib.h> void handler(int signo) { printf(First statement); system(date); exit(EXIT_SUCCESS); } int main()
My C code #include <stdio.h> #include <stdlib.h> #include help.h int test(int x, P *ut)
Test the following code: #include <stdio.h> #include <stdlib.h> main() { const char *yytext=0; const
I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>
I have a program here: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ipc.h> #include

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.