0

Ok cant get my head around this one :/
But basically i need to write a program that accpets input from a keyboard "JACK ROBINSON" then it manipulates the string to output INK SON ROB JAC...

Heres what i have so far:

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

void main()
{
     char *FullName="JACK ROBINSON";
     char *Input;
     char *Word1, *Word2, *Word3, *Word4;
     
do
{     
          printf("Please enter JACK Robinson: ");
          scanf("%s", Input);
          
          if(strcmp(Input,FullName)==0)// Checks to see if user entered "JACK ROBINSON" correctly
          {
              strncpy(Word4,FullName,3); // Copies JAC to Word4
              Word4[3] = '\0';
               //rest of manipulation to go here                       
          }
          else
          {
            printf("Please Try Again\n");
            }
}
while (strcmp(Input,FullName)!=0);

}

Now atm it crashes when i type in JACK ROBINSON or any incorrect data... any ideas why its doing this and also how to get the 3 other answers by manipulatin the string?
Many thanks,
Mark Hebden

6
Contributors
9
Replies
11
Views
11 Years
Discussion Span
Last Post by WaltP
0

if thats wat u want to do... jumbling up the letters of something then why dont u just transfer them to different arrays as u rquire and finish ur work of.. tht shudnt be difficult..

0

>void main()
*smack* Main returns int.

>atm it crashes when i type in JACK ROBINSON or any incorrect data...
I can answer your question with another question: Where do your pointers point? If you said "I don't know", you're right, and that's why your program crashes.

0

Thank you =)
Never been able to get my head around pointers but now i think ive cracked it :)
Ok heres what i got now

#include <stdio.h>
#include <string.h>
#define WORDSIZE 4

int main()
{
     char *FirstName="JACK", *LastName="ROBINSON";
     char *Input1, *Input2;
     char *Word1, *Word2, *Word3, *Word4;
     Word1 = (char *) malloc (WORDSIZE);
     Word2 = (char *) malloc (WORDSIZE);
     Word3 = (char *) malloc (WORDSIZE);
     Word4 = (char *) malloc (WORDSIZE);
     Input1 = (char *) malloc (5);
     Input2 = (char *) malloc (9);
do
{     
          printf("Please enter JACK: ");
          scanf("%s",Input1);
          
          if(strcmp(Input1,FirstName)==0)// Checks to see if user entered "JACK ROBINSON" correctly
          {
             do
             {
                printf("Please enter ROBINSON: ");
                scanf("%s",Input2);
          
                if(strcmp(Input2,LastName)==0)// Checks to see if user entered "JACK ROBINSON" correctly
                {                                                      
                strncpy(Word4,FirstName,3); // Copies JAC to Word4
                Word4[3] = '\0';
                
                strncpy(Word3,LastName,3); // Copies ROB to Word4
                Word3[3] = '\0';
                
               //rest of manipulation to go here 
             
               printf("Your name is now: %s\t %s\t %s\t %s\t", Word1, Word2, Word3, Word4);                      
                }
                else
                {
                printf("Please Try Again\n");
                }
          }
          while (strcmp(Input2,LastName)!=0);
          }
          else
          {
            printf("Please Try Again\n");
          }
}
while (strcmp(Input1,FirstName)!=0);

}

So any ideas on how to get INK and SON?

0

>now i think ive cracked it
You also need to free the memory you allocate. And don't cast the result of malloc. That way you'll get a warning telling you that you forgot to include stdlib.h. Why are you using pointers in the first place? In this case you could use arrays with not problem at all.

>So any ideas on how to get INK and SON?
That depends. Will this be a general algorithm that should work with any input? Or will you just be working with JACK ROBINSON in all possible cases?

0

It will always be JACK ROBINSON its an exam question. The pointers need to be there to show i know how to use them same with the strcmp and strcpy.
So basically it just needs to work as a one off. Stupid yea i know but its the way they want it.

0
printf("INK SON ROB JAC");

What I don't understand is how JACK ROBINSON becomes INK SON ROB JAC. How does the K end up placed in INK?

0

Dont ask me lol that what the question says... i have no idea how to do it :/

-1

use strtok(firstname,"JAC") to get the string: "K\0" lets say its inkSEG1
like this: char *inkSEG1=strtok(FirstName,"JAC");
then char *strSEG2=strtok(LastName, "ROB");
now u have "K\0" and "INSON\0"
copy first 2 letters from strSEG2 to new segment lets say: strncpy(word1, strSEG2, 2);
rigth now all u need is to add them so..
strncat(word1,strSEG1,1);
now u have in word1 "INK"
to get "SON" is even easier look:
strSEG2= strtok(strSEG2,"IN");
now that we got "SON\0"
do this:
strncpy(word2,strSEG2,3);
and tadaaaaaaa u finished youre assingment!!!!!
hope i wasnt to late thought :P

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.