Cours:CorrigeFonctionC : Différence entre versions
| (2 révisions intermédiaires par le même utilisateur non affichées) | |||
| Ligne 1 : | Ligne 1 : | ||
| + | =tableau de char= | ||
| + | <source lang=c> | ||
| + | /****************************************************************************** | ||
| + | |||
| + | Online C Compiler. | ||
| + | Code, Compile, Run and Debug C program online. | ||
| + | Write your code in this editor and press "Run" button to compile and execute it. | ||
| + | |||
| + | *******************************************************************************/ | ||
| + | |||
| + | #include <stdio.h> | ||
| + | |||
| + | int tailleString(const char texte[]) | ||
| + | { | ||
| + | int pos=0; | ||
| + | while( texte[pos] != 0) pos++; | ||
| + | return pos; | ||
| + | } | ||
| + | |||
| + | void toUpperCase(char texte[]) | ||
| + | { | ||
| + | int nb=tailleString(texte); | ||
| + | int i; | ||
| + | for (i=0;i<nb;i++) | ||
| + | { | ||
| + | if ((texte[i]>='a')&&(texte[i]<='z')) | ||
| + | texte[i]=texte[i]-('a'-'A'); // ou -32 | ||
| + | } | ||
| + | } | ||
| + | |||
| + | int main() | ||
| + | { | ||
| + | char message[100]="Td CHAINE de caracteres"; | ||
| + | int nb = tailleString(message); | ||
| + | printf("il y a %d caracteres dans le message : %s\n",nb,message); | ||
| + | toUpperCase(message); | ||
| + | printf("message en majuscule : %s",message); | ||
| + | |||
| + | return 0; | ||
| + | } | ||
| + | |||
| + | </source> | ||
| + | |||
| + | =tableau d'entiers= | ||
<source lang=c> | <source lang=c> | ||
int afficheTab(int tab[],int tailleTab) | int afficheTab(int tab[],int tailleTab) | ||
Version actuelle datée du 11 novembre 2020 à 22:20
tableau de char
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
int tailleString(const char texte[])
{
int pos=0;
while( texte[pos] != 0) pos++;
return pos;
}
void toUpperCase(char texte[])
{
int nb=tailleString(texte);
int i;
for (i=0;i<nb;i++)
{
if ((texte[i]>='a')&&(texte[i]<='z'))
texte[i]=texte[i]-('a'-'A'); // ou -32
}
}
int main()
{
char message[100]="Td CHAINE de caracteres";
int nb = tailleString(message);
printf("il y a %d caracteres dans le message : %s\n",nb,message);
toUpperCase(message);
printf("message en majuscule : %s",message);
return 0;
}
tableau d'entiers
int afficheTab(int tab[],int tailleTab)
{
int i;
for (i=0;i<tailleTab;i++)
{
printf("%d ",tab[i]);
}
printf("\n");
}
int maxTab(int tab[],int tailleTab)
{
int maxi;
int i;
if (tailleTab>0)
{
maxi=tab[0];
for (i=1;i<tailleTab;i++)
{
if (tab[i]>maxi) maxi=tab[i];
}
}
return maxi;
}
int minTab(int tab[],int tailleTab)
{
int mini;
int i;
if (tailleTab>0)
{
mini=tab[0];
for (i=1;i<tailleTab;i++)
{
if (tab[i]<mini) mini=tab[i];
}
}
return mini;
}