Thread: Simple function that I tried to build.

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

    Simple function that I tried to build.

    Hi, I just started to learn C 5 hours ago.
    I tried to build a simple function that simulates the substr() function of JavaScript, php, etc.

    now lets look at the problem:
    let me say, my string is: I S A A C.
    and I make sub(mystr, 0, 2).
    Ok, the program write's me: ISA (Until here its all ok..) and a lot of @#$%^& (This is the problem).

    Now, I know that is because the others characters didnt received any value, but I havent any idea how to solve the problem.

    The code:
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int strll(char str[10]);
    void sub(char string[10], int min, int max);
    
    int main()
    {
    	char str[10];
    	printf("String: ");
    	gets(str);
    	printf("\n");
    	sub(str,0,2);
    	
    	return 0;
    }
    
    int strll(char str[10])
    {
    	int i;
    
    	for(i = 0; str[i] != '\0'; i++);
    	return i;
    }
    
    void sub(char string[10], int min, int max)
    {
    	char strcp[10];
    	int i, j;
    	j = 0;
    
    	for(i = min; i <= max; i++)
    	{
    		strcp[j] = string[i];
    		j++;
    	}
    		
    	printf("%s",strcp);
    }
    or : http://scripter.fresh.li/substr.txt

    Thanks,
    Isaac.

  2. #2
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Hmmm ... I don't get any strange output.
    But the output for string 'I S A A C' is not 'ISA' ... it's just 'I S' but that's obvious.
    And what about your function 'strll' ... you don't use it?!
    Code:
    deleted
    Last edited by Sargnagel; 10-23-2002 at 04:13 PM.

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

    What you gave me is just to echo a string

    I want a function Like substr() (Substring)..
    Thanks for trying to help.
    Last edited by Scripter; 10-23-2002 at 03:18 PM.

  4. #4
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    @String echo:
    oops .. sorry. You're right! Just a few minutes ...

    What compiler/OS are you using?
    Last edited by Sargnagel; 10-23-2002 at 03:21 PM.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    12

    I fixed the problem :)

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int strll(char str[10]);
    void sub(char string[10], int min, int max);
    
    int main()
    {
    	char str[10];
    	printf("String: ");
    	gets(str);
    	printf("\n");
    	sub(str,0,2);
    	
    	return 0;
    }
    
    int strll(char str[10])
    {
    	int i;
    
    	for(i = 0; str[i] != '\0'; i++);
    	return i;
    }
    
    void sub(char string[10], int min, int max)
    {
    	char strcp[10];
    	int i, j,k;
    	j = 0;
    
    	for(i = min; i <= max; i++)
    	{
    		strcp[j] = string[i];
    		j++;
    	}
    	
    
       for(i = 0; i <= max; i++)
    	   printf("%c",strcp[i]);
    
       printf("\n");
    	
    	
    }
    Not bad to a 5 hour's programmer(newbie) in C, right?

  6. #6
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Damn, you were faster.
    Really not bad for a 5 h newbie!
    But anyway here is my interpretation of the substring() function of JAVA. It's very basic and not secure!

    Code:
    #include <stdio.h>
    
    int strll(char str[]);
    void sub(char string[], int min, int max);
    
    int main()
    {
    	char str[10];
    	int start, end;
    	printf("String: ");
    	gets(str);
    	printf("\nstart: ");
    	scanf("%d", &start);
    	printf("\nend: ");
    	scanf("%d", &end);
    	
    	sub(str,start-1,end-1);
    	
    	return 0;
    }
    
    void sub(char string[], int min, int max)
    {
    	int i;
    	
    	for(i = min; i <= max; i++)
    	{
    		printf("%c", string[i]);
    	}	
    }
    Last edited by Sargnagel; 10-23-2002 at 03:51 PM.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    12

    Thanks Again for the Help!

    Thanks.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @Scripter

    A couple of pointers for you:

    Names begining str followed by a letter are reserved for implementation use, so steer clear of them.

    gets() is a bad function to use, lookup fgets() and learn how to use that. gets() is a very typical starting point for a newbie, but get over it as soon as you can.

    char str[10]; <-- is way too small for an input buffer from the user, and the good old gets() function will exploit that weakness.

    Have fun
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    12

    Ok, I got it.

    I will try soon as I can to start using fgets() instead gets().
    Can you give me an example, how to use fgets()?

    On this example:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char name[100];
    	printf("Please insert your name: ");
    	gets(name);
    	printf("\nYour name is %s, right?\n",name);
    
    	return 0;
    }
    How could, I use in this example, fgets() instead gets()?
    Thanks!

  10. #10
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    Here's somewhere you might refer to:

    http://www.rt.com/man/fgets.3.html

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    12

    dbaryl and Salem, Thanks.

    Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM