Search code examples
cstringascii

Find ascii of every character in a string split by '-'


So, i want to make a program in c where you input a string then the output will be ascii number of every character in that string split by '-'. i almost done then i dont know how to remove the last '-' in the output of my code. exp. input: abc output: 97-98-99. Here is my code :

    char s[1001];
    
    scanf("%s",&s);
    for(int i=0;s[i]!='\0';i++){
        printf("%d-",s[i]);
    }
    return 0;
}

My output : 97-98-99-


Solution

  • Slightly rearrange your logic:

    for(int i=0;s[i]!='\0';i++){
      if( i != 0 )
        printf("-");
      printf("%d",s[i]);
    }