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 8102171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:10:39+00:00 2026-06-05T23:10:39+00:00

I had earlier written a program in java which takes in as input a

  • 0

I had earlier written a program in java which takes in as input a string as an input and checks if it is valid or not. The rules for deciding are:

1) The string is recognizable if and only if it contains the words “pi”, “ka”,”chu” as its fragments, in any order repeated any number of times .


2) If it contains any other fragments( or subsequence), Then it is unrecognizable


I was able to do so in java a bit easily as java has better support for string functions. My code is (this works fine)


import java.util.Scanner;
public class RecognisingWords { 
public static void main(String[] args) throws Exception 
{
    Scanner inp= new Scanner(System.in);
    String str;
    System.out.println("Enter the string to be tested:");
    str=inp.nextLine();

    while(str.length()>0)
    {

    if(str.startsWith("pi"))
    {
        str= str.substring(2);          
    }

    else if(str.startsWith("ka"))
    {
        str= str.substring(2);          
    }

    else if(str.startsWith("chu"))
    {
        str= str.substring(3);          
    }

    else
    {
        System.out.println("Unrecognisable Sequence");
        break;
    }

    }

    if(str.length()==0)
    {
        System.out.println("Recognisable Sequence");
    }   
}
}

However when i am writing the corresponding program in c (using pointers), my code runs into infinite loop. Please check my code and point wher the error is. Also is it possible to implement this program without using pointer ??


#include <stdio.h>
#include <stdlib.h>
#include<string.h>

int main(void)
{
char str[10];
int len=0,flag=1;
char *ptr;

printf("Enter the string : ");
gets(str);
len=strlen(str);
ptr=str;

while(*ptr!='\0')
{
    if(!strcmp("pi",ptr))
    {
        ptr=ptr+2;
    }

    else if(!strcmp("ka",ptr))
    {
        ptr=ptr+2;
    }

    else if(!strcmp("chu",ptr))
    {
        ptr=ptr+3;
    }

    else
    {
        printf("String not recognised");
        flag=0;
        break;
    }
}

if(flag==1)
    printf("String is recognised");

return 0;
}

I have corrected some of my very silly mistakes. Hope You people don’t mind

  • 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-06-05T23:10:40+00:00Added an answer on June 5, 2026 at 11:10 pm

    You probably want to advance your pointer by ptr += 2 or ptr += 3 rather than changing the character pointed by the pointer: *ptr = ....

    The logic in the loop is also not correct. You should loop until the current position of the pointer points to 0 (NUL character – which is the string terminator in C), by while (*ptr != '\0'). You seem to have missed out the break in the last else block.

    There is also the problem with gets(str), which may cause buffer overflow problem in C. Security stuffs aside, your program will behave unexpectedly if you enter a string longer than 9 characters.

    There are potentially other problems with your C code that I cannot list out by just eyeballing your code.

    EDIT

    Another problem is that you used strcmp, which will compare everything pointed to by the pointer with the fragment. i.e. If the input is “pikachu”, it will compare “pi” with “pikachu” and finds that “pi” is lexically smaller than “pikachu” and it will show that the string is not recognized. You probably want strncmp, in which you can specify the number of characters to compare.

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

Sidebar

Related Questions

Earlier I had a problem when an inner anonymous class did not see a
Earlier this week, I had to do something which feels like a semantics violation.
I had an earlier question about integrating Mathematica with functions written in C++. This
Are there any gotchas in JDK 6 which did not exist in earlier versions?
I had earlier posted a query on Java threads. ( link text ) And
extending the question I had asked earlier which can be found here, plotting multiple
I had posted this earlier on Stack Overflow, but couldn't get a positive result.
I had posted one question earlier jQuery inconsistency in setting readonly attribute in IE-8
I had my server working with an earlier version of node.js, npm, and socket.io
I had developed a script, earlier I use to transfer it directly without any

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.