How to do this ?

This is a discussion on How to do this ? within the C Programming forums, part of the General Programming Boards category; Write a function "no_repetitions(...)" which removes all repetitions of characters from a string. Test the function in a suitable main ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    16

    How to do this ?

    Write a function "no_repetitions(...)" which removes all repetitions of characters from a string. Test the function in a suitable main program, which should be able to reproduce the following input/output

    Type in a string: This string contains repeated characters
    The string without repetitions is: This trngcoaepd

    I failed to solve this problem and i cannot ype the imput with space, i mean i can only type the imput like Thisstringcontainsrepeatedcharacters

    Can anyone help me ?

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    16
    Code:
    #include<stdio.h>
    
    char x[100],y[1];
    
    
    int main()
    
    {
    
    	printf("Type in the string : ");
    
    	scanf("%c",&x);
    
    	for(int k=0;k<1000;k++)
    
    	for(int i=0;i<100;i++)
    
    	{	y[1]=x[i];
    
    
    
    	for(int j=i+1;j<100;j++)
    
    		if(y[1]==x[j])
    
    		{
    
    				for(int l=j;l<100;l++)
    
    				x[l]=x[l+1];
    
    		break;
    
    		}
    
    	}
    
    	printf("The string without repeatitions is %c",x);
    
    return 0;
    
    }
    This is what i transfer from c++, but it cannot work, how to change it ?

    &#91;code]&#91;/code]tagged by Salem

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >scanf("%c",&x);
    Your code only reads a single character, try this instead:
    Code:
    #include <stdlib.h> /* Add this header */
    .
    .
    .
    if ( fgets ( x, sizeof x, stdin ) == NULL ) {
      fputs ( "No input or error reading input\n", stderr );
      exit ( EXIT_FAILURE );
    }
    /* The rest of the program here */
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    16
    How to add in ?? I had try but cannot

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >#include<stdio.h>
    Add the following line after this one:

    #include <stdlib.h>

    >scanf("%c",&x);
    Replace this line with the following:
    Code:
    if ( fgets ( x, sizeof x, stdin ) == NULL ) {
      fputs ( "No input or error reading input\n", stderr );
      exit ( EXIT_FAILURE );
    }
    >I had try but cannot
    I would wager that you didn't try very hard.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    16
    I had try that but my program still cannot run, thats y i ask someone to tell me how to add in
    Is that my program is wrong or wad?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is that my program is wrong or wad?
    Yes, it is. In more ways than just trying to plug in a piece of code. For starters, you are trying to write C++ code. From my understanding, you want the string with no repetitions to go in y, yet you print x, the logic of the loops can be simplified greatly with the use of the standard libraries as well.

    Compare this with what you have, be sure to compile the program as C and not C++, try walking through each line and determine what it does, then test to see if that is what it does:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ( void )
    {
      int i, len, current = 0;
      char input[100], output[100] = {0};
      
      fputs ( "Type in the string : ", stdout );
      if ( fgets ( input, sizeof input, stdin ) == NULL ) {
        fputs ( "Error reading input or no input\n", stderr );
        exit ( EXIT_FAILURE );
      }
      
      len = strlen ( input );
      for ( i = 0; i < len; i++ ) {
        if ( memchr ( output, input[i], current ) == NULL )
          output[current++] = input[i];
      }
      
      printf ( "The string without repeatitions is: %s", output );
      
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    16
    Your program really work !! But for my standard, it is very hard to let me understand, can you tell me the way that how you start to study C and master it ?

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>can you tell me the way that how you start to study C and master it ?
    Read books, perform lots of research, writing small programs that get progressively more complex. And as a last resort, after trying everything on my own, ask someone to help me.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >can you tell me the way that how you start to study C and master it ?
    Use it, read about it, and ask a lot of questions. It never hurts to lurk around knowledgeable people and just watch what they do and say as well.

    -Prelude
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    16
    Thank you very much~

Popular pages Recent additions subscribe to a feed

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