Thread: string problems

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    4

    string problems

    hello, i would like to know how can i put a '?' at the begining of a digit sequence ended by 2.

    i've made a code but it doesn't work, this is what i did:

    Code:
    #include<stdio.h>
    
    	main() {
      
    		char input[51];
    		char digits[]="0123456789";
    		scanf("%s", input);
    		int i;
    		int j;
      	
    			for(i=0; input[i]!='\0'; i++) {
          	
    				if((input[i]==digits[2])) {  
    				       
    					for(input[i]=2; i>input[0]; i--) {
    		 
    						if(input[i]!=digits[j]) { 
    		   
    						input[i]='?'; 
    						}
    					}
    				}
    			}
    	
    		printf("%s\n", input );
     
    	}
    there is no ouput for this code, i've tried some other ways but it just didn't work

    if somebody can help me I will apreciate

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Look into ctype.h's isdigit() function: http://www.hmug.org/man/3/isnumber.html
    That should make your code more clean for you to easily see what to do from there.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >i would like to know how can i put a '?' at the begining of a digit sequence ended by 2.

    Maybe something like this.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       int i;
       for ( i = 0; i < 15; ++i )
       {
          char input [ 51 ], *ptr = input + 1;
          sprintf(ptr, "%d", i); /* fake user input */
          if ( input [ strlen(input) - 1 ] == '2' )
          {
             *--ptr = '?';
          }
          puts(ptr);
       }
       return 0;
    }
    
    /* my output
    0
    1
    ?2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    ?12
    13
    14
    */
    Last edited by Dave_Sinkula; 10-22-2004 at 09:15 AM. Reason: [1] Added color. [2] Changed to simpler output using puts(). [3] Decreased loop iterations.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Dave_Sinkula says...

    Maybe something like this.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       int i;
       for ( i = 0; i < 15; ++i )
       {
          char input [ 51 ], *ptr = input + 1;
          sprintf(ptr, "%d", i); /* fake user input */
          if ( input [ strlen(input) - 1 ] == '2' )
          {
             *--ptr = '?';
          }
          puts(ptr);
       }
       return 0;
    }
    I dont think so, not in C programming anyway.
    Code:
    for ( i = 0; i < 15; ++i )
       {
          /* illegal in C, ok in C++ */
          char input [ 51 ], *ptr = input + 1;
          sprintf(ptr, "%d", i); /* fake user input */
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >I dont think so, not in C programming anyway.

    Think again.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by bigtamscot
    I dont think so, not in C programming anyway.
    Code:
    for ( i = 0; i < 15; ++i )
       {
          /* illegal in C, ok in C++ */
          char input [ 51 ], *ptr = input + 1;
          sprintf(ptr, "%d", i); /* fake user input */
    What's so illegal about that? It's perfectly valid C.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. io stream and string class problems
    By quizteamer in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2005, 12:07 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM