c - Add '-' between each character in a string? -
how that? willing add character -
after every character in string. specific case assume have string declared in size of 100 filled 3 letters (entered user - value of character str[3]
null (\0
)).
for(i = strlen(str) ; >= 0 ; i--) { str[i*2] = str[i]; } for(i = 0 ; < strlen(str) ; i++) { if(i % 2 != 0) { str[i] = '-'; } }
but it's not working, anyone?
#include <stdio.h> #include <string.h> int main() { char str[100]; int len = 0; int = 0; strcpy(str, "abcd"); len = strlen(str); // len = 4 // expected output: "a-b-c-d" // str[7] = '\0'; // str[6] = 'd'; for(i = len-1; >= 0 ; i--) { str[i*2+1] = '-'; str[i*2] = str[i]; } str[len*2-1] = '\0'; printf("%s\n", str); }
Comments
Post a Comment