Jump to content

Como eliminar um caracter numa string?


Crazyluigi
 Share

Recommended Posts

Boas!!

Estou com um problema pois queria eliminar um caracter numa string introduzida por teclado sendo também o utlizador a introduzir o caracter.

void main()
{
	char car;
	char pal[DIM];
	char palm[DIM];
	int i,j;

	printf(" Insira um caracter : ");
	scanf("%c",&car);
	printf(" Insira uma linha de texto :");
	scanf("%s",&pal);
	j=strlen(pal);
		for(i=0; i<=j; i++)
		{
			if (pal[i]!=car)
				palm[i]=pal[i];
			else
			{
				palm[i]=pal[i+1];

			}
		}
	puts(palm);
	getche();

esta versão já elimina o caracter mas fico com uma repetição de caracter seguinte...

Link to comment
Share on other sites

1º não consegui ver bem o que estavas a dizer com o recuar pois já tinha tentado... mas depois tive mais uma pequena ajuda e tá feito!!!

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define DIM 30



void main()
{
	char car;
	char pal[DIM];
	char palm[DIM];
	int i,j, k=0;

	printf(" Insira um caracter : ");
	scanf("%c",&car);
	printf(" Insira uma linha de texto :");
	scanf("%s",&pal);
	j=strlen(pal);
		for(i=0; i<=j; i++)
		{
			if (pal[i]!=car)
				palm[k]=pal[i];
			else
			{
				palm[k]=pal[i+1];
				k--;
			}
		k++;
		}
	puts(palm);
	getche();

Eis o resultado final!!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.