1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
char *insertion(char*Pt_Chaine,int Position,char Caractere)
{
char *Pt_debut;
char *Pt_copie;
Pt_copie=(char*)malloc(100*sizeof(char));
int i=0;
Pt_debut=Pt_copie;
int taille =strlen(Pt_Chaine);
printf("voici la taille de la chaine:%d\n",taille);
printf("Pt_Chaine:%s Position:%d Caractere:%c\n",Pt_Chaine,Position,Caractere);
while(i < taille+1)
{
if (i < Position-1)
{
*(Pt_copie+i)=*(Pt_Chaine+i);
}
else if (i == Position-1)
{
*(Pt_copie+i)=Caractere;
}
else
{
*(Pt_copie+i)=*(Pt_Chaine+i-1);
}
i++;
}
printf("%s\n",&*Pt_debut);
return;
}
char *suppression(char*Pt_Chaine,int Position)
{
char *Pt_debut;
char *Pt_copie;
Pt_copie=(char*)malloc(100*sizeof(char));
int i=0;
Pt_debut=Pt_copie;
int taille =strlen(Pt_Chaine);
printf("voici la taille de la chaine:%d\n",taille);
printf("Pt_Chaine:%s Position:%d\n",Pt_Chaine,Position);
while(i < taille+1)
{
if (i < Position-1)
{
*(Pt_copie+i)=*(Pt_Chaine+i);
}
else
{
*(Pt_copie+i)=*(Pt_Chaine+i+1);
}
i++;
}
printf("%s\n",&*Pt_debut);
return;
}
int main (void)
{
char *Pt_Chaine;
Pt_Chaine=(char*)malloc(100*sizeof(char));
int c;
int choix;
printf("entrer la chaine\n");
gets(Pt_Chaine);
while(choix)
{
printf("1_Affichage chaine saisie\n");
printf("2_Insertion d'un caractere a une position donnee\n");
printf("3_Suppression d'un caractere a une position donee\n");
printf("4_Quitter le programme\n");
printf("que voule vous faire\n");
scanf("%d",&choix);
if (choix==1)
{
printf("%s\n",&*Pt_Chaine);
}
if(choix==2)
{
printf("INSERTION\n");
int Position;
char Caractere;
printf("entrer la position d' insertion du caractere\n");
scanf("%d",&Position);
getchar();
printf("entree le caractere a insere\n");
scanf("%c",&Caractere);
insertion(Pt_Chaine,Position,Caractere);
getch();
}
if(choix==3)
{
printf("SUPRESSION\n");
int Position;
printf("entrer la position de suppression du caractere\n");
scanf("%d",&Position);
getchar();
suppression(Pt_Chaine,Position);
getch();
}
if(choix==4)
{
free(Pt_Chaine);
break;
}
}
} |
Partager