Like Tree1Likes
  • 1 Post By laserlight

Cant figure out what's wrong with my code..

This is a discussion on Cant figure out what's wrong with my code.. within the C Programming forums, part of the General Programming Boards category; I have an assignment: Write a function which would determine whether a string is valid or not by the following ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    20

    Cant figure out what's wrong with my code..

    I have an assignment:
    Write a function which would determine whether a string is valid or not
    by the following rules:
    1.The string must include only the letters: 'x', 'y' and 'z'.
    2.The string must end with 'z'.
    3.Every time a 'y' is written it must come with another one attached 'yy'.
    4.The number of 'x's must be odd.
    e.g
    Valid word: yyxyyxzxz
    Invalid word: xyxyxz

    Here is the code I wrote:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    bool isValid (char s[])
    {
    	int count_x=0;
    
    
    	if (s[(strlen(s)-1)]!='z') return false;
    
    
    	for (int i=0; i<strlen(s);i++)
    	{
    		if (s[i]!='x' && s[i]!='y' && s[i]!='z') return false;
    		if (s[i]=='x') count_x++;
    		if ((s[i]=='y' && s[i+1]!='y') && (s[i]=='y' && s[i-1]!='y')) return false;
    	}
    	if (count_x%2==0) return false;
    	else return true;
    
    
    }
    
    
    int main (void)
    {
    	char s1[10]="xxyyxzyz\0", s2[10]="helloxyz\0";
    
    
    	if (isValid(s1)) printf ("s1 - valid\n");
    	else printf ("s1 - invalid\n");
    
    
    	if (isValid(s2)) printf ("s2 - valid\n");
    	else printf ("s2 - invalid\n");
    }
    Please help me to figure out what is wrong
    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    > 3.Every time a 'y' is written it must come with another one attached 'yy'.
    You supposedly correct string fails this test.

    Now is a good time to start learning how to use the debugger.

    For example, you would put a breakpoint on the start of isValid, then single step the code until it returned with an unexpected false (there are plenty to choose from).

    Having found which it is, you can examine the associated condition and discover the source of the bug.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    20
    I didnt understand, can you write the code you meant instead of my line 17?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Here's the solution:
    Code:
    bool isValid(char s[])
    {
        return hasOnly(s, "xyz") && endsWith(s, 'z') && comesAttached(s, 'y') && countChar(s, 'x') % 2 != 0;
    }
    I leave the implementation of the functions called as an exercise to the reader, but my point here is that if you break up the problem into smaller parts, then it can become easier to solve and debug. A trade-off is that it could also be less efficient, but if the difference is negligible for your purposes, then the gain outweighs the cost.

    Incidentally, the implementation of hasOnly is trivial if you are allowed to use strspn from <string.h>.
    iMalc likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2013
    Posts
    20
    A'right thanks to you all.!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help? I can't figure out what's wrong with this?
    By kaiser2614955 in forum C Programming
    Replies: 24
    Last Post: 07-29-2011, 08:15 PM
  2. Help! I cannot figure out what I'm doing wrong here!
    By chsindian595 in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 04:18 AM
  3. I cant figure out what is wrong with this
    By C++angel in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2006, 10:57 PM
  4. please help, i can't figure out what's wrong
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2002, 10:13 PM
  5. Replies: 0
    Last Post: 11-11-2001, 01:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21