0

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-

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
suppatra
  • 17
  • 4
  • 1
    you can use `strlen` function to obtain the length of your input and than do not print the dash when you are at the last char – piertoni Mar 30 '22 at 08:14
  • Welcome to SO. Please do not change the scope of your question after you got answers. Generally, each post shall only contain one question. If you want to ask different things, please ask a new question. I will rollback your edit. – Gerhardh Mar 30 '22 at 09:17
  • @piertoni a `strlen()` call is not needed here. – chux - Reinstate Monica Mar 30 '22 at 12:59
  • @chux-reinstate-monica when a guy is learning you don't have and don't need to suggest the more efficient solution (that probably is obscure), but the most easy to understand. Everyone can catch a concepts with a different example... so I prefer to add a bit o variety than stick to common solutions. – piertoni Mar 30 '22 at 14:00

4 Answers4

4

We'll use Ternary Operator, to check whether i is the last character before '\0' by checking s[i + 1] != 0, here i is the current index of the string. Here's a visual representation.

NOTE: Your variable char s[1001] will be decayed to a pointer (exception: sizeof operator).

Some improvements:

  • Don't use "%s", use "%<WIDTH>s", to avoid buffer-overflow
  • Use size_t to iterate through an array.
  • Instead of using bare return 0;, use return EXIT_SUCCESS;, which is defined in the header file stdlib.h.
  • always check whether scanf() input was successful or not

Final Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
    char s[1001] = {0};
    if (scanf("%1000s", s) != 1)
    {
        perror("bad input");
        return EXIT_FAILURE;
    }
    for (size_t i = 0; s[i]; i++)
        printf("%d%s", s[i], (s[i + 1]) ? "-" : "");
    puts("\n");
    return EXIT_SUCCESS;
}

Output:

Hello
72-101-108-108-111
Darth-CodeX
  • 2,166
  • 1
  • 6
  • 23
  • You can use a define for the array size, and '%*s' with sizeof in scanf in order to avoid "magic number" 1000 and 1001. You don't need the variable "len" either, so you can simplify this code further. I know, i'm nitpicking ... – Tom's Mar 30 '22 at 09:51
  • Because you can do `for (size_t i = 0; s[i]; i++)` for the loop (since '\0' is equal to 0) and you can do `(s[i + 1]) ? "-" : ""` for the ternary. – Tom's Mar 30 '22 at 10:01
2

Slightly rearrange your logic:

for(int i=0;s[i]!='\0';i++){
  if( i != 0 )
    printf("-");
  printf("%d",s[i]);
}
infinitezero
  • 1,610
  • 3
  • 14
  • 29
  • thank you for the answer, may i ask again what if i want to add some number test case for the input what the code will be ? for ex. input : 2 abc hello output : Case#1. 97-98-99 Case#2. 104-101-108-108-111 – suppatra Mar 30 '22 at 08:33
0

check if current index is not the last index then print - otherwise it's the last index and print newline

char s[1001];

scanf("%s",&s);

int len = 0;

for(;s[len]!='\0';len++);

for(int i=0;i<len;i++){
    printf("%d",s[i]);
    if(i+1 != len)
       printf("-")
    else
        printf("\n")
}
Udesh
  • 2,415
  • 2
  • 22
  • 32
  • I think my solution is way easier and less complex – infinitezero Mar 30 '22 at 08:16
  • 2
    Solutions are just a personal approach and making such comments is not in the spirit of SO. I suggest that you restrain yourself from making such in the future. – Gnqz Mar 30 '22 at 08:30
  • @Gnqz I think the less complex and hence easier approach is a fact. There was no disrespect intended. – infinitezero Mar 30 '22 at 15:44
  • SO uses a voting system and this is how the higher quality content gets distinguished from the rest. I'm by no means a SO representative neither I intend to disrespect your own opinion, but if you are willing to take a piece of advice from me - let your answers/content speak for itself, there is no need to compare it to the others. – Gnqz Mar 31 '22 at 07:36
0

Instead of using strlen(), length or repeated tests in a loop, simply change the separator.

const char *separator = "";
for (size_t i=0; s[i]!='\0'; i++) {
    printf("%s%d",separator, s[i]);
    separator = "-";
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256