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.
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.
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
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.
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.