Thread: strcat

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    strcat

    If I were to concatenate five strings, and I can pick pick any two randomly, how would I do it.

    note: I tried, but the compiler gave me atleast 22 errors. Any kind of help will be greatly appreciated.

  2. #2
    Unregistered
    Guest
    spose you could create an array of 5 integers and in each of those integers store the starting position of each string as there joined together, ie

    int startpos[5];
    int counter, beginchar, endchar;

    startpos[0]=0;
    tempstring="WHATEVER"; //string1
    startpos[1]=strlen(tempstring);
    strcat(tempstring,"HELLO"); //string2
    startpos[2]=strlen(tempstring);
    strcat(tempstring,"THIS"); //string3
    startpos[3]=strlen(tempstring);
    strcat(tempstring,"SHOULD"); //string4
    startpos[4]=strlen(tempstring);
    strcat(tempstring,"WORK"); //string5

    then produce a random number
    srand((unsigned) time(&t)); // reposition the random position.
    beginchar = rand()%5;
    if(beginchar==4) // Check if its the last string
    endchar=strlen(tempstring);
    else
    endchar=startpos[beginchar+1];

    for(counter=beginchar;counter<endchar;counter++)
    printf("%c",tempstring[counter];


    this code should work, havent ran it, may of missed some ; or somehin small but hope it helps
    Enjoy,
    Bull

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    please help

    I'm very new to programming. I signed up for programming in C, but I guess the teaching level is too high for me. I got most of my program done, and it compiles, but just got stuck on few things. If anyone could solve these qustions, I'll be very grateful.
    I tried but couldn't succeed.

    Five strings are entered by a user. They successfully display on the screen. Now the user have five strings, and he can select any two randomly.

    1. concatenate two strings. The program will prompt the user for the two strings to concatenate, then display the resulting string. It will then allow the user to replace one of the five strings with this new string.


    2. Reverse a string. The program will prompt the userfor the sting number to reverse, then replace that sting with the reversed string.

  4. #4
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    You might want to make an array of strings and let the user input the string numbers to concatenate. Then you can just replace the number to be used as array subscripts for the string array, then pass the string array elements to the 'strcat' function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting question about strcat
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2009, 12:59 PM
  2. strcat the inside of a structure
    By ebullient in forum C Programming
    Replies: 3
    Last Post: 12-09-2005, 05:58 PM
  3. strcat and a char
    By jjacobweston in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2005, 04:10 PM
  4. strcat makes program crash
    By imoy in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2002, 02:41 PM
  5. Removing 'strcat' ed string.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-14-2001, 12:09 AM