Here is the piece of code. Its in C, compiler is CodeBlocks I made a function Replacethings which is supposed to replace all characters that are spaces, commas, and exclamation points with *. Seemingly an easy task, and yet so hard. The input has to be 25 characters or less.
void Replacethings( char *StrongOfChars[25]){
for(int i =0; i<25; i ){
if(*StrongOfChars[i]=' ' || *StrongOfChars[i] = ',' || *StrongOfChars[i] = '!'){
*StrongOfChars[i]= '*';
printf("%c", StrongOfChars[i]);
}
}
}
printf("Enter your favorite quote (Has to be less than 25 characters)!");
char StronKK[25];
scanf("%s", &StronKK);
Replacethings(&StronKK);
printf("\n Your favorite squote is now: %s ", StronKK);
CodePudding user response:
*StrongOfChars[i]=' '
means you assign the value ' ' to the first character of the i'th char pointer:
if(*StrongOfChars[i]=' ' || *StrongOfChars[i] = ',' || *StrongOfChars[i] = '!'){
You want to use ==
instead. The first expression of the body of the if
statement is:
*StrongOfChars[i]= '*';
which probably should be part of your if expression instead, i.e.:
if(
*StrongOfChars[i] == ' ' ||
*StrongOfChars[i] == ',' ||
*StrongOfChars[i] == '!' ||
*StrongOfChars[i] == '*'
) {
...
}
or you can use strchr(" ,!*", *StrongOfChars[i])
instead of those 4 conditions.
The code is poorly formatted, so you cannot see that the }
before the 2nd print statement is in the wrong place so the print statement is not in a function. Maybe remove it or you forgot a main()
function?
CodePudding user response:
As you declared the array StronKK
like
char StronKK[25];
then the expression &StronKK
has the type char ( * )[25]
.
So you have to write in the call of scanf
scanf( "$s", StronKK );
On the other hand, the function parameter has the type char *StrongOfChars[25]
void Replacethings( char *StrongOfChars[25])
that is adjusted by the compiler to the type char **
void Replacethings( char **StrongOfChars )
The types char ( * )[25]
and char **
are not compatible types and the compiler should issue a diagnostic message.
You need to declare the function like
void Replacethings( char *StrongOfChars )
or
void Replacethings( char StrongOfChars[25])
or
void Replacethings( char StrongOfChars[])
and call it like
Replacethings( StronKK );
Pay attention to that the user can enter a string with the length less than 25
. So using the magic number 25
within the function is a bad idea and can result in undefined behavior.
Also instead of the equality operator ==
you are using the assignment operator =
within the for loop of the function.
The function can be defined the following way
void Replacethings( char StrongOfChars[])
{
for ( ; *StrongOfChars != '\0'; StrongOfChars )
{
if ( *StrongOfChars == ' ' ||
*StrongOfChars == ',' ||
*StrongOfChars == '!' )
{
*StrongOfChars = '*';
}
}
}
It would be also reasonable to check whether a character is a tab character. That is the if statement can look
if ( *StrongOfChars == ' ' ||
*StrongOfChars == '\t' ||
*StrongOfChars == ',' ||
*StrongOfChars == '!' )
{
*StrongOfChars = '*';
}
Or you can write it lime
if ( strchr( " \t,!", *StrongOfChars ) )
{
*StrongOfChars = '*';
}
To use the function strchr
you need to include header <string.h>
.