Loading lang_c_05_strings...
... { char txt[10]={72, 105, 44, 9, 69, 78, 73, 66, 33, 10}; // saisie des codes ASCII for(int i=0; i<10; ++i) { printf("%c", txt[i]); // Hi, ENIB! } for(int i=0; i<10; ++i) { printf("%d ", txt[i]); // 72 105 44 9 69 78 73 66 33 10 }
... { char txt[10]={'H', 'i', ',', '\t', 'E', 'N', 'I', 'B', '!', '\n'}; // saisie littérale for(int i=0; i<10; ++i) { printf("%c", txt[i]); // Hi, ENIB! } for(int i=0; i<10; ++i) { printf("%d ", txt[i]); // 72 105 44 9 69 78 73 66 33 10 } ... }
<ctype.h>propose encore bien d'autres fonctions pour distinguer plus finement ces catégories.
... { char txt[7]={'1', ' ', 'M', 'o', 'R', 'e', '?'}; for(int i=0; i<7; ++i) { if(isdigit(txt[i])) { printf("<%c>", txt[i]); // <1> } } for(int i=0; i<7; ++i) { if(ispunct(txt[i])) { printf("<%c>", txt[i]); // <?> } } for(int i=0; i<7; ++i) { if(isupper(txt[i])) { printf("<%c>", tolower(txt[i])); // <m><r> } } ... }
... { char txt[12]={'B', 'h', 'e', ' ', 'F', 'r', 'p', 'e', 'r', 'g', '!', '\n'}; for(int i=0; i<12; ++i) { char c=txt[i]; if(isalpha(c)) { int first=isupper(c) ? 'A' : 'a'; c=(char)(first+((c-first)+13)%26); } printf("%c", c); // Our Secret! } ... }
... { char txt[]={'H', 'e', 'l', 'l', 'o', ',', ' ', 'E', 'N', 'I', 'B', '!', '\n', '\0'}; for(int i=0; txt[i]; ++i) { printf("%c", txt[i]); // Hello, ENIB! } ... }
... { char txt[]={'H', 'e', 'l', 'l', 'o', ',', ' ', 'E', 'N', 'I', 'B', '!', '\0'}; printf("<%s>\n", txt); } // <Hello, ENIB!> ... }
... { char txt1[]={'H', 'e', 'l', 'l', 'o', ',', ' ', 'E', 'N', 'I', 'B', '!', '\0'}; char txt2[]="Hello, ENIB!"; printf("<%s><%s>\n",txt1,txt2); // <Hello, ENIB!><Hello, ENIB!> ... }
... { char txt1[]="Only" " one " "string"; char txt2[]="This is a very long text that would hardly fit the width of" " my text editor! Fortunately, the designers of C thought about " "every detail."; printf("<%s><%s>\n", txt1, txt2); // <Only one string><This is a very long text ... every detail.> ... }
... { char txt[]="Hello, ENIB!"; char *split=txt+5; for(char *p=txt; p<split; ++p) { *p=(char)toupper(*p); // passer les cinq premières lettres en majuscule } *(split++)='\0'; // marquer la fin juste après puis avancer printf("<%s><%s>\n", txt, split); // <HELLO>< ENIB!> ... }
... { const char *txt="Hello, ENIB!"; printf("<%s><%s>\n", txt, txt+7); // <Hello, ENIB!><ENIB!> ... }
... { char txt1[]="correct"; char txt2[]={'c', 'o', 'r', 'r', 'e', 'c', 't', '\0'}; const char *txt3="correct"; // const char *txt4={'i', 'n', 'c', 'o', 'r', 'r', 'e', 'c', 't', '\0'}; // INCORRECT ... }
<string.h>est justement dédié à la déclaration de fonctions spécialisées dans leur manipulation.
... { char str1[]="first string"; char str2[100]="second string"; const char *str3="third string"; printf("<%s> %d %d\n", str1, (int)strlen(str1), (int)sizeof(str1)); // <first string> 12 13 printf("<%s> %d %d\n", str2, (int)strlen(str2), (int)sizeof(str2)); // <second string> 13 100 printf("<%s> %d %d\n", str3, (int)strlen(str3), (int)sizeof(str3)); // <third string> 12 8 printf("<%s> %d %d\n", str2+7, (int)strlen(str2+7), (int)sizeof(str2+7)); // <string> 6 8 ... }
... { char str1[]="same string"; const char *str2="same string"; char *str3=str1; if(str1!=str2) { printf("str1!=str2\n"); // str1!=str2 } if(str1==str3) { printf("str1==str3\n"); // str1==str3 } ... }
... { char str1[]="shorter", str2[]="same string"; const char *str3="same string", *str4="same string but longer"; printf("%d %d %d\n", // 7 0 -32 strcmp(str1, str2), strcmp(str2, str3), strcmp(str3, str4)); if(strcmp(str2, str3)==0) { printf("str2 same as str3\n"); // str2 same as str3 } ... }
... { char txt[]="demonstrate"; char *p=strchr(txt, 'e'); if(p!=NULL) { *p='E'; // changer le premier 'e' } p=strrchr(txt, 't'); if(p!=NULL) { *p='T'; // changer le dernier 't' } p=strstr(txt, "str"); if(p!=NULL) { *p='S'; // changer le 's' de "str" } p=strchr(txt, 's'); if(p==NULL) { printf("no '%c' in <%s>\n", 's', txt); // no 's' in <dEmonStraTe> } ... }
... { const char *str1="first string"; char str2[100]; // très largement dimensionné strcpy(str2, str1); printf("<%s>\n", str2); // <first string> strcpy(str2+6, str1); printf("<%s>\n", str2); // <first first string> ... }
... { char str1[100]="one way"; char str2[100]="another way"; strcat(str1, " to concatenate"); strcpy(str2+strlen(str2), " to do the same"); printf("<%s><%s>\n", str1, str2); // <one way to concatenate><another way to do the same> ... }
int main(int argc, // argument counter : le nombre de mots de la ligne de commande char **argv) // argument values : le tableau des mots de la ligne de commande { // ... return 0; }
... { char a0[]="./prog", a1[]="there", a2[]="are", a3[]="some", a4[]="options"; char *commandLine[]={a0, a1, a2, a3, a4, NULL}; int result=main(5, commandLine); ... }
#include <stdio.h>
int main(int argc, char **argv) { for(int i=0; i<argc; ++i) // argv[0]=<./prog> { // argv[1]=<there> printf("argv[%d]=<%s>\n", i, argv[i]); // argv[2]=<are> } // argv[3]=<some> return 0; // argv[4]=<options> }
<stdlib.h>.
#include <stdio.h>
int main(int argc, char **argv) { if(argc<2) { printf("Is there anybody out there?\n"); } else if(argc==2) { printf("Hello %s!\n", argv[1]); printf("Don't you feel alone?\n"); } else if(argc==3) { printf("Hello %s and %s!\n", argv[1], argv[2]); printf("It is a pleasure to meet you both.\n"); } else { printf("Hello %s", argv[1]); for(int i=2; i<argc-1; ++i) { printf(", %s", argv[i]); } printf(" and %s!\n", argv[argc-1]); printf("It is a pleasure to meet you all.\n"); } return 0; }