-
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 ?
:(
-
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 ?
[code][/code]tagged by Salem
-
>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
-
How to add in ?? I had try but cannot
-
>#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
-
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?
-
>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
-
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 ?
-
>>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.
-
>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
-