Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The program should accept the words aba and all words of even length such as abababb it should give valid answer but it is not giving....
Here is the code:
C#
#include<conio.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main(void)
{
clrscr();
int i=0;
char str[30];
printf("Enter the input string: ");
gets(str);
if(str[i]=='a')
{
str[i++];
if(str[i]=='b')
{
str[i++];
if(str[i]=='a')
{
str[i++];
if(str[i]==NULL)
{
printf("\nVALID");
}
else
{
loop:
;
if(str[i]=='a'|| str[i]=='b')
{
str[i++];
goto loop;
}
else
{
goto a;
}
a:
;
if(i%2==0)
{
printf("\nVALID");
}
else
{
printf("\nINVALID");
}
}
}
else
{
printf("\nINVALID");
}
}
else
{
printf("\nINVALID");
}
}
else
{
printf("\nINVALID");
}
getch();
}


Please correct my code??</string.h></stdio.h></conio.h>
Posted
Updated 28-Mar-14 21:20pm
v2
Comments
[no name] 29-Mar-14 3:39am    
(1) "all words of even length such as abababb" ?
(2) goto ?
(3) Requirement is not clear - is it pass "aba" and all even length words and fail all others?
OriginalGriff 29-Mar-14 4:38am    
Under normal circumstances, I'm pretty laid back, but I'm not even going to *start* looking at that!
Do two things first:
1) Indent your code. This makes it a lot, lot more readable - particularly if you didn't write it, as is the case here.
2) Remove the word "goto" from your memory, and then from your code: use a while loop instead. At the moment, your code is unstructured "spaghetti" which is probably contributing to the problem you are having. You do not need goto for this task, and will in all probability never need goto at all. Certainly, you should never use it until you have a few years real-world experience under your belt and understand *when* it is appropriate to use.
Use the "Improve question" widget to edit your question and provide better information.

1 solution

one bug is:
str[i++];
//its is enough
i++;


the bug is

if(i%2==0)


for abababb, coz its uneven.

Consider improving your codestyle. Really :mad:
 
Share this answer
 

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