Thread: Error

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    21

    Error

    hi
    i have coded a program such that when a string with a hyphen (-) is entered it should display all the letters between characters on either side of string.
    input is harsha-gas
    output is a b c d e f g

    but i am getting an error in the program....could anyone pls help

    thanks

    also in case if input is harsha-9gas then it should print a-z then A-Z in caps and 0-9

    how do i do this

    could anyone give me a clue

    thanks
    Code:
    #include<stdio.h>
    #include<string.h>
    main()
    {
     char a[100],p,q;
     int i,j,k,s;
     printf("enter the string seperated by a -");
     
    gets(a);
     printf("%s",a);
    s=strlen(a);
    for(i=0;i<s;i++)
    {
    	if(a[i]=='-')
    	{
    		p=a[i-1];
    		q=a[i+1];
    	}
    
    	if(p>=97&&p<=123)
    	{
    		printf("%c ",p++);
    	}
    	if(p==q)
    		break;
    	else
    		if(p>=65&&p<=91)
    		{
    			printf("%c ",p++);
    		}
    }
    printf("%c",q);
    }

    then it must print
    Last edited by Salem; 08-28-2007 at 10:22 AM. Reason: [code] at the start, [/CODE] at the end. Use preview next time to make sure you honor the intent, not just make the popup go

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see any loop besides the for-loop that scans through the input string.
    I also don't think the braces around your if (a[i] == '-')) are in the right place.

    Could you use the "code" tags to make your code look a bit more readable?

    --
    Mats

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    gets(a);
    Use fgets instead of gets:
    Code:
    fgets(a,sizeof(a),stdin);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM