You are on page 1of 2

© Mr HousNi * housni14@gmail.

com

Exemple de Manipulation des Chaines de Caractères

#include <stdio.h>
#include <stdlib.h>
#include<string.h>

int main(void)
{
char mot1[100], mot2[100];
int l, n;

printf(" Donner mot1:\n");


gets(mot1);
printf(" Donner mot2:\n");
gets(mot2);

// concaténation
strcat(mot1,mot2);
printf(" Concatenation de mot1 et mot2:\n");
puts(mot1);

// longeur de la concaténation
l=strlen(mot1);
printf(" Longueur de la concatenation:\n%d",l);

// inverser la concatenation
strrev(mot1);
printf("\n Concatenation inverse:\n");
puts(mot1);

// comparaison alphabétique
printf(" Comparaison alphabetique de %s et %s\n",mot1,mot2);
if(strcmp(mot1,mot2)>0) {puts(mot2); puts(mot1);}
if(strcmp(mot1,mot2)==0) printf("identique");
if(strcmp(mot1,mot2)<0) {puts(mot1); puts(mot2);}

// copie de mot2 dans mot1


strcpy(mot1,mot2);
printf(" Copie de mot2 dans mot1:\n");
puts(mot1);

// existence de a dans mot1, idem strrchr en commençant par la fin


printf(" Existence de 'a':");
if(strchr(mot1,'a')!=NULL) printf("\n'a' existe!");
else printf("\n'a' n'existe pas!");

// existence de ab dans mot1


printf("\n Existence de 'ab':");
if(strstr(mot1,"ab")!=NULL) printf("\n'ab' existe!");
else printf("\n'ab' n'existe pas!");

printf("\n");
system("PAUSE");
}

1/2
© Mr HousNi * housni14@gmail.com

Capture d'écran

2/2

You might also like