Thread: Is my main function correct?

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    10

    Is my main function correct?

    I have to split(s,c,sl,sr)-splits a string s into two substrings sl and sr. Sl is the portion of the string to the left of c and sr is the portion following c. If the string does not contain c, sl is a copy of s and sr is empty string. This is a utility function called by other functions. It returns true if c is found in s, otherwise it returns false. S and c are input parameters, and sl and sr are output parameters. Assuming that all parameters are strings.

    Code:
    split(char s; char c; char sl; char sr) 
    int main(void) 
    {
       int i = 0; 
       char sl, sr, s, c;
       char *chptr, name, value;
       char sentence[15][80], a[10], b[100]; 
    
       printf("Please enter a URL."); 
       fgets(sentence, 80, stdin);
       for(chptr = sentence; *chptr != '\n'; chptr++);
       *chptr = '\0';
    
       while(i>7)
       { 
           sentence[i] = a[i]; 
           i++; 
       }
    
       if(strcmp(a[i], "http://"))
       { 
           exit(0);
       }
    
       while(sentence[i] != b[i])
       {
           sentence[i] = b[i]; 
           i++;
       }
       printf("The domain name:", b[i]);

    I'm new to "C" programming so I don't know clearly what I'm doing.

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Here's the best way to find out if it's correct:

    test it.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    26

    Thumbs up

    Dear friend,
    In your main function you are comparing the string using the following.
    strcmp(a[i],"http://").
    But the array 'a' is not having any value.
    So if you run this program it will produce segmentation fault.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    split(char s; char c; char sl; char sr)     // this is a syntax error. no return type missing semicolon
    int main(void) 
    {
       int i = 0; 
       char sl, sr, s, c;
       char *chptr, name, value;
       char sentence[15][80], a[10], b[100]; 
    
       printf("Please enter a URL."); 
       fgets(sentence, 80, stdin);              // there are 15 sentences  you have to choose one for input
       for(chptr = sentence; *chptr != '\n'; chptr++);
       *chptr = '\0';
    
       while(i>7)                               // this loop will either not be entered ( if i < 8 ) or it will loop forever
       { 
           sentence[i] = a[i];                  // you cannot assign a char to a string
           i++; 
       }
    
       if(strcmp(a[i], "http://"))              // you cannot compare a char and a string
       { 
           exit(0);
       }
    
       while(sentence[i] != b[i])               // you cannot compare a char and a string
       {
           sentence[i] = b[i];                  // you cannot assign a char to a string
           i++;
       }
       printf("The domain name:", b[i]);        // incorrect format string
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Return Statement
    By Daveo in forum C Programming
    Replies: 21
    Last Post: 11-09-2004, 05:14 AM