Thread: simple program with text output using string operations

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87

    simple program with text output using string operations

    Hi. I am working on a program that would have nothing on input and it will produce 100 lines and on each of them there will be written a string that is a result of combining without any space the following:

    firststring+5+thirdstring

    where instead of the 5 there will be a string being numerically equal to the value of the i variable from the cycle. Here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
         
    int main(void)
    {
        int i;
    //    char t[100];
        for(i=1;i<101;i++)
        {
          // t[]="firststring"+i+"thirdstring";
          char t=concat("firststring", String.valueOf(i), "thirdstring");
          printf("%s\n",t);
        }
        return 0;
    }
    I have found that I can convert integer i to a string using String.valueOf(i) and that I can concetenate strings using concat() function. I also have troubles printing the resulted string. What do I do wrong?

    Here is what compiler wrote:

    prog.c: In function 'main':
    prog.c:12:14: warning: implicit declaration of function 'concat' [-Wimplicit-function-declaration]
    char t=concat("firststring", String.valueOf(i), "thirdstring");
    ^
    prog.c:12:36: error: 'String' undeclared (first use in this function)
    char t=concat("firststring", String.valueOf(i), "thirdstring");
    ^
    prog.c:12:36: note: each undeclared identifier is reported only once for each function it appears in
    prog.c:13:14: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=]
    printf("%s\n",t);
    ^
    Last edited by nerio; 01-18-2016 at 06:13 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    String.valueOf(i)
    That is not C. I think it might be Java.

    Look up "sprintf()". This should do exactly what you need.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple c program-please explain output.
    By hitesh.incept in forum C Programming
    Replies: 6
    Last Post: 09-13-2012, 12:04 PM
  2. Simple question about text files and strings
    By Gabriel Sotero in forum C Programming
    Replies: 1
    Last Post: 03-02-2012, 07:36 PM
  3. Output Problem, Very simple program
    By MrAlt in forum C Programming
    Replies: 6
    Last Post: 01-15-2012, 08:34 PM
  4. Simple text output to the clipboard
    By phantom in forum Windows Programming
    Replies: 4
    Last Post: 12-14-2009, 02:02 AM
  5. Weird output simple program.
    By omnificient in forum C++ Programming
    Replies: 7
    Last Post: 11-03-2005, 01:53 PM