0% ont trouvé ce document utile (0 vote)
22 vues13 pages

Manipulation de tableaux en C

Le document présente plusieurs exemples de code C illustrant l'utilisation de pointeurs, de tableaux et de chaînes de caractères en C.

Transféré par

hamzaberriche332
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats RTF, PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
22 vues13 pages

Manipulation de tableaux en C

Le document présente plusieurs exemples de code C illustrant l'utilisation de pointeurs, de tableaux et de chaînes de caractères en C.

Transféré par

hamzaberriche332
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats RTF, PDF, TXT ou lisez en ligne sur Scribd

ex2:

#include <stdio.h>

#include <stdlib.h>

void read_array(int** array, int* size) {

printf("Enter size of array: ");

scanf("%d", size);

*array = (int*)malloc(*size * sizeof(int));

for (int i = 0; i < *size; i++) {

printf("Enter element %d: ", i + 1);

scanf("%d", &(*array)[i]);

void append_array(int** array1, int* size1, int** array2, int size2) {

*array1 = (int*)realloc(*array1, (*size1 + size2) * sizeof(int));

for (int i = 0; i < size2; i++) {

(*array1)[*size1 + i] = (*array2)[i];

*size1 += size2;

int main() {

int* array1;

int size1;
read_array(&array1, &size1);

int* array2;

int size2;

read_array(&array2, &size2);

append_array(&array1, &size1, &array2, size2);

printf("Updated array: ");

for (int i = 0; i < size1; i++) {

printf("%d ", array1[i]);

printf("\n");

free(array1);

free(array2);

return 0;

ex3:

*P+2 : La valeur de l'élément du tableau A à l'index 0 + 2, donc 12 + 2, égale 14.

*(P+2) : La valeur de l'élément du tableau A à l'index 2, donc 34.


&P+1 : L'adresse de P (l'adresse du pointeur P) + 1, dépend du système d'exploitation et de la machine.

&A[4]-3 : L'adresse de l'élément du tableau A à l'index 4 (56) - 3, dépend du système d'exploitation et de


la machine.

A+3 : L'adresse du tableau A + 3 (le troisième élément du tableau), dépend du système d'exploitation et
de la machine.

&A[7]-P : La différence entre l'adresse de l'élément du tableau A à l'index 7 (90) et l'adresse de P.

P+(*P-10) : L'adresse actuelle de P + (la valeur pointée par P, c'est-à-dire A[0]) - 10.

ex4:

#include <stdio.h>

#include <stdlib.h>

void read_array(int** array, int* size) {

printf("Enter size of array: ");

scanf("%d", size);

*array = (int*)malloc(*size * sizeof(int));

for (int i = 0; i < *size; i++) {

printf("Enter element %d: ", i + 1);

scanf("%d", &(*array)[i]);

void remove_all_occurrences(int** array, int* size, int X) {


int* P1 = *array;

int* P2 = *array;

int count = 0;

for (int i = 0; i < *size; i++) {

if ((*P1) != X) {

*(P2++) = *(P1);

count++;

P1++;

*size = count;

*array = (int*)realloc(*array, *size * sizeof(int));

int main() {

int* array;

int size;

read_array(&array, &size);

int X;

printf("Enter value to remove: ");

scanf("%d", &X);
remove_all_occurrences(&array, &size, X);

printf("Updated array: ");

for (int i = 0; i < size; i++) {

printf("%d ", array[i]);

printf("\n");

free(array);

return 0;

ex5:

#include <stdio.h>

void reverse_array(int* array, int size) {

int* P1 = array;

int* P2 = array + size - 1;

int AIDE;

while (P1 < P2) {

AIDE = *P1;

*P1 = *P2;

*P2 = AIDE;

P1++;
P2--;

int main() {

int array[] = {1, 2, 3, 4, 5};

int size = sizeof(array) / sizeof(array[0]);

reverse_array(array, size);

printf("Reversed array: ");

for (int i = 0; i < size; i++) {

printf("%d ", array[i]);

printf("\n");

return 0;

ex6:

#include <stdio.h>

#include <string.h>

#include <ctype.h>

int length_of_string(char* str) {

char* P = str;
while (*P != '\0') {

P++;

return P - str;

int main() {

char CH[100];

printf("Enter a string: ");

fgets(CH, sizeof(CH), stdin);

int length = length_of_string(CH);

printf("Length of string: %d\n", length);

return 0;

ex7:

a/#include <stdio.h>

#include <string.h>

#include <ctype.h>

int is_palindrome(char* CH) {

int length = strlen(CH);

for (int i = 0; i < length / 2; i++) {

if (tolower(CH[i]) != tolower(CH[length - i - 1])) {


return 0;

return 1;

int main() {

char CH[100];

printf("Enter a string: ");

fgets(CH, sizeof(CH), stdin);

if (is_palindrome(CH)) {

printf("The string is a palindrome.\n");

} else {

printf("The string is not a palindrome.\n");

return 0;

b/

#include <stdio.h>

#include <string.h>

#include <ctype.h>

int is_palindrome(char* CH) {


char* start = CH;

char* end = CH + strlen(CH) - 1;

while (start < end) {

if (tolower(*start) != tolower(*end)) {

return 0;

start++;

end--;

return 1;

int main() {

char CH[100];

printf("Enter a string: ");

fgets(CH, sizeof(CH), stdin);

if (is_palindrome(CH)) {

printf("The string is a palindrome.\n");

} else {

printf("The string is not a palindrome.\n");

}
return 0;

}#include <stdio.h>

#include <string.h>

#include <ctype.h>

int is_palindrome(char* CH) {

char* start = CH;

char* end = CH + strlen(CH) - 1;

while (start < end) {

if (tolower(*start) != tolower(*end)) {

return 0;

start++;

end--;

return 1;

int main() {

char CH[100];

printf("Enter a string: ");

fgets(CH, sizeof(CH), stdin);


if (is_palindrome(CH)) {

printf("The string is a palindrome.\n");

} else {

printf("The string is not a palindrome.\n");

return 0;

ex8:

#include <stdio.h>

#include <string.h>

#include <ctype.h>

int count_letters(char* CH) {

int ABC[26] = {0};

char* PCH = CH;

while (*PCH != '\0') {

if (isalpha(*PCH)) {

ABC[tolower(*PCH) - 'a']++;

PCH++;

int count = 0;
for (int i = 0; i < 26; i++) {

if (ABC[i] > 0) {

count++;

return count;

int main() {

char CH[100];

printf("Enter a string: ");

fgets(CH, sizeof(CH), stdin);

int letters_count = count_letters(CH);

printf("Number of letters: %d\n", letters_count);

return 0;

ex9:

int count = 0;

while (count < 10 && fgets(CH, sizeof(CH), stdin) != NULL) {

CH[strcspn(CH, "\n")] = '\0';

MOT[count] = (char*)malloc(strlen(CH) + 1);

strcpy(MOT[count], CH);

count++;
}

qsort(MOT, count, sizeof(char*), compare_strings);

while (count > 0) {

printf("%d words remaining:\n", count);

for (int i = 0; i < count; i++) {

printf("%d: %s\n", i + 1, MOT[i]);

printf("Press 'Enter' to delete the first word: ");

getchar();

getchar();

free(MOT[0]);

for (int i = 0; i < count - 1; i++) {

MOT[i] = MOT[i + 1];

count--;

free_words(MOT, 10);

return 0;

Vous aimerez peut-être aussi