Given a number and some sort of string.
It is necessary to do the following: (easier by example). For example, the number 5 and the string "abc" .
It is necessary to display the following set of lines:

"abc-1", "abc-2", "abc-3", "abc-4", "abc-5".

These strings should be stored somewhere (in an array of strings).

Understandably, this is not a difficult task, but how can this be done more simply and optimally in C?

Closed due to the fact that the question’s essence is not clear to the participants from Vlad from Moscow , αλεχολυτ , pavel , Denis , Denis Bubnov Jan 20 '17 at 6:25 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

    2 answers 2

    Given a comment entry in the array, try this:

     #include <stdio.h> int main(void) { int x = 10; // данное число char* s = "Stroka"; // данная строка char arr[x][255]; for (int i = 0; i < x; i++) { sprintf(arr[i], "%s-%d", s, i+1); // запись в элемент массива arr[i] printf("%s ", arr[i]); } return 0; } 

    Conclusion:

    Stroka-1 Stroka-2 Stroka-3 Stroka-4 Stroka-5 Stroka-6 Stroka-7 Stroka-8 Stroka-9 Stroka-10

    An example .

    • one
      abc not always the same) and why do i + 1 if you can initially set the cycle i=1 - tCode
    • @tCode thank you, inattentively read) - Denis
    • And int argc, char *argv[] for what? And, and with commensurately large x everything will go to hell. Try malloc something. - 0andriy

    If you just need to output, then in the cycle from 1 to this number write

     printf("abc-%d", num); 

    where num is the loop counter.

    Well, or if the string is also variable, then

     printf("%s-%d", str, num); 
    • No, you need to save it to a variable. That is, we have an array of strings, and the resulting strings must be saved in the array - marka_17
    • @ marka_17. So maybe this should be described in the question ??? - Sublihim
    • one
      @ marka_17, then you can simply use the function sprintf , instead of printf - iksuy
    • @iksuy, yes, for sure .. I somehow did not guess - marka_17