The code you share removes all the repeated characters in the string, but you only want to delete some characters (the points) after the first point.
For this you could follow the following algorithm:
Go through the read string
If the character is not a point
Copy the character to the result
If the character is a point
If it is the first point that is found
Copy the point to the result
The code would be like this:
#include <stdio.h>
#include <string.h>
int main()
{
char resultado[27]="";
char palabra[50];
int i=0,j=0;
int punto = 0; // variable centinela para comprobar el número de puntos
printf("Ingresa una frase!\n");
scanf("%s",palabra);
// atraviesa la palabra leida
for (i = 0; i < strlen(palabra); i++) {
// si la palabra es un punto
if (palabra[i] == '.') {
// y es el primer punto que se encuentra
if (punto == 0) {
// activa el centinela y añade el punto al resultado
punto = 1;
resultado[j++] = '.';
}
// si no es un punto, añade el carácter/número al resultado
} else {
resultado[j++] = palabra[i];
}
}
resultado[j] = '#include <stdio.h>
#include <string.h>
int main()
{
char resultado[50] = "";
char palabra[50];
int i=0,j=0;
char *subcadena;
printf("Ingresa una frase!\n");scanf("%s",palabra);
// si se encuentra un punto en la palabra
if (subcadena = strchr(palabra, '.')) {
// actualiza los valores necesitados
i = subcadena - palabra + 1;
memcpy( resultado, palabra, i);
j = strlen(resultado);
}
// atraviesa la palabra
while(palabra[i])
{
// solo insertando los caracteres que no son puntos
if(palabra[i] != '.')
resultado[j++]=palabra[i];
i++;
}
resultado[j]='#include <stdio.h>
#include <string.h>
int main()
{
char resultado[27]="";
char palabra[50];
int i=0,j=0;
int punto = 0; // variable centinela para comprobar el número de puntos
printf("Ingresa una frase!\n");
scanf("%s",palabra);
// atraviesa la palabra leida
for (i = 0; i < strlen(palabra); i++) {
// si la palabra es un punto
if (palabra[i] == '.') {
// y es el primer punto que se encuentra
if (punto == 0) {
// activa el centinela y añade el punto al resultado
punto = 1;
resultado[j++] = '.';
}
// si no es un punto, añade el carácter/número al resultado
} else {
resultado[j++] = palabra[i];
}
}
resultado[j] = '#include <stdio.h>
#include <string.h>
int main()
{
char resultado[50] = "";
char palabra[50];
int i=0,j=0;
char *subcadena;
printf("Ingresa una frase!\n");scanf("%s",palabra);
// si se encuentra un punto en la palabra
if (subcadena = strchr(palabra, '.')) {
// actualiza los valores necesitados
i = subcadena - palabra + 1;
memcpy( resultado, palabra, i);
j = strlen(resultado);
}
// atraviesa la palabra
while(palabra[i])
{
// solo insertando los caracteres que no son puntos
if(palabra[i] != '.')
resultado[j++]=palabra[i];
i++;
}
resultado[j]='%pre%';
printf("\nEl resultado seria!\n%s",resultado);
getchar();
return(0);
}
';
printf("\nEl resultado seria!\n%s",resultado);
getchar();
return(0);
}
';
printf("\nEl resultado seria!\n%s",resultado);
getchar();
return(0);
}
';
printf("\nEl resultado seria!\n%s",resultado);
getchar();
return(0);
}
A slightly different alternative that looks more like the code you showed:
Initializes the variables i
and j
to 0.
Initializes variable resultado
to "".
Use strchr
to find the first point in the string read
If a point was found
Update the value of i
to the position of the point in the string
Update the value of resultado
to the substring to the point (you can use memcpy
for it)
Update the value of j
to the length of resultado
Traverse the string read from position i
.
If the character is not a point, add it to the result
If you look at steps 1, 2 and 5, they are (almost) the same as what you already have, and you would only have to add steps 3 and 4, which are to update the variables used in step 5 and following.
The code would be like this:
%pre%