C-Kurs/concat/Musterlösung
< C-Kurs | concat
Version vom 13. September 2011, 21:17 Uhr von 77.185.26.106 (Diskussion)
#include <stdio.h> #include <stdlib.h> int string_length(char* string) { int result = 0; while(string[result]!='\0') result++; return result; } char* concat(char *string1, char *string2) { int len1 = string_length(string1); int len2 = string_length(string2); char* result = malloc(len1+1+len2+1); int i; for(i=0; i<len1; i++) result[i] = string1[i]; result[len1] = ' '; for(i=0;i<len2;i++) result[len1+i+1] = string2[i]; result[len1+1+len2] = '\0'; return result; } int main(int argc,char** argv) { printf("TEST: %s\n",concat("Wort1","Wort2")); //Zusatz if(argc>=3) { char *current = concat(argv[1],argv[2]); char *result = current; int i; for(i=3; i<argc; i++) { result = concat(current,argv[i]); free(current); current = result; } printf("PARAMETER: %s\n",result); }else{ printf("Nicht genug Parameter"); } return 0; }