randomlock
randomlock

Reputation: 29

replacing a character in string in C

Have to replace a user input character with another user input character and print the string . What am i doing wrong ?

#include<stdio.h>
#include<conio.h>
main()
{
int i;
char a,b,str[100];
printf("Enter the string");
gets(str);
//asking for replacement
printf("enter the character to be replaced");
scanf("%c",&a);
// which letter to replace the existing one
printf("enter the character to replace");
scanf("%c",&b);
for(i=0;str[i]!='\0';i++)
{
    if(str[i]==a)
    {
    str[i] = b;
    }
    else
    continue;
}
printf("the new string is");
puts(str);
}

Upvotes: 2

Views: 37087

Answers (4)

Spikatrix
Spikatrix

Reputation: 20244

#include<stdio.h>
#include<conio.h>
int main() //main returns an int
{
int i;
char a,b,str[100];

printf("Enter the string\n");
fgets(str,sizeof(str),stdin);//gets is dangerous

printf("Enter the character to be replaced\n");
scanf(" %c",&a); //space before %c is not neccessary here

printf("Enter the character to replace\n");
scanf(" %c",&b); //space before %c is compulsory here
for(i=0;str[i]!='\0';i++)
{
    if(str[i]==a)
    {
    str[i] = b;
    }
    //else //This part is not neccessary
    //continue;
}
printf("The new string is ");
puts(str);
return 0; //main returns an int
}

I've used fgets because gets is dangerous as it does not prevent buffer overflows.

The space before %c in the scanf is to skip blanks,i.e,spaces,new-lines etc and it isn't needed in the first scanf is that fgets also consumes the new-line characters and puts it into the buffer.

The reason that the else continue; isn't needed is that the loop is going to check the condition as it has reached the end of the loop body.

I've used int main() and return 0 because as per the latest standards,it should

Finally,you have an unused header conio.h in your program.

Upvotes: 1

Chandru
Chandru

Reputation: 1334

Add getchar() function between the two scanf().

Like

#include<stdio.h>
main()
{
        int i;
        char a,b,str[100];
        printf("Enter the string");
        gets(str);
        //asking for replacement
        printf("enter the character to be replaced");
        scanf("%c ",&a);
        //Get the pending character.
        getchar();
        // which letter to replace the existing one
        printf("enter the character to replace");
        scanf("%c",&b);
        for(i=0;str[i]!='\0';i++)
        {
                if(str[i]==a)
                {
                        str[i] = b;
                }
                else
                        continue;
        }
        printf("the new string is");
        puts(str);
}

The problem is when you give a character and pressed enter, newline will acts as one character and it will be get by the next scanf. To avoid that the getchar() is using.

Another Way:

Give space before the access specifier on character to replace,

Like

scanf(" %c",&b);

But before remove that getchar().

Upvotes: 1

Prerak Sola
Prerak Sola

Reputation: 10009

Try this, it worked for me:

#include<stdio.h>
#include<conio.h>
main()
{
  int i;
  char a,b,str[100];
  printf("Enter the string: ");
  gets(str);
  //asking for replacement
  printf("enter the character to be replaced: ");
  a = _getch();
  printf("\n%c", a);
  // which letter to replace the existing one
  printf("\nenter the character to replace: ");
  b = _getch();
  printf("\n%c", b);

  for(i=0;str[i]!='\0';i++)
  {
    if(str[i]==a)
    {
      str[i] = b;
    }

    else
     continue;
  }
  printf("\nthe new string is: "); 
  puts(str);
}

You can remove the else block. It won't affect anything.

Upvotes: 0

Numby
Numby

Reputation: 135

scanf("%d",&a);

You get an integer ? not a character ? If it is a character, then you should use %c instead of %d

Upvotes: 2

Related Questions