pls help

This is a discussion on pls help within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <conio.h> #include <string.h> void strtoken(char *s1,char *s2) { int num; for(num=0;num<strlen(s1);num++) { if(s1[num]==s2) //error printf("\n"); else ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    Post pls help

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    
    void strtoken(char *s1,char *s2) {
    	 int num;
    	 for(num=0;num<strlen(s1);num++) {
    	       if(s1[num]==s2)      //error
    		  printf("\n");
                           else 
                              printf("%c",s1[num]);
    	 }
    	     }
    void main() {
         char sentence[256],ch[1];
         clrscr();
         printf("Enter a sentence:\n");
         gets(sentence);
         printf("Enter a character:");
         scanf("%c",ch[0]);
         strtoken(sentence,ch);
         getch();
         }
    error is :can not convert char to *char
    what can i do to run this program

    for example my text is :this is a sentence
    the output should be:
    this
    is
    a
    sentence if the seperation key is "blank"
    Last edited by condorx; 05-20-2002 at 03:30 PM.

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > error is :can not convert char to *char
    ...along with void main, and not returning an int from main.
    The world is waiting. I must leave you now.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    9
    try,

    if(s1[num]==*s2)

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, I fixed your original code, to a limited extent. It's pretty unsafe, so I wrote another version for you to review as well.

    This is a working verion based on yours
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    void strtoken(char *s1, char *s2)
    {
    	int num;
    	int len = strlen(s1);
    
    	for (num = 0; num < len; num++)
    	{
    		if (s1[num] == *s2)
    			printf("\n");
    		else
    			printf("%c", s1[num]);
    	}
    }
    
    int main(void)
    {
    	char	sentence[256], ch[1];
    	clrscr();
    	printf("Enter a sentence:\n");
    	gets(sentence);  /* very bad, don't use this! */
    	printf("Enter a character:");
    	scanf("%c", ch); /* Also not the best, avoid */
    	strtoken(sentence, ch);
    	getch();
    	return (0);
    }
    And this is a safer version:
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    void strtoken(char *, char);
    
    void strtoken(char *s1, char ch)
    {
    	int num;
    	char *ptr;
    
    	for (ptr = s1; *ptr != '\0'; ptr++)
    	{
    		if (*ptr == ch)
    			putchar('\n');
    		else
    			putchar(*ptr);
    	}
    }
    
    int main(void)
    {
    	char sentence[256], ch;
    	clrscr();
    	printf("Enter a sentence:\n");
    	if (fgets(sentence, 256, stdin) != NULL)
    	{
    		printf("Enter a character:");
    		if ((ch = getchar()) != EOF)
    			strtoken(sentence, ch);
    	}
    	getch();
    	return (0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    Post why do we use fgets

    why dont we use gets and we use fgets?i think stdin is used for input

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

    Re: why do we use fgets

    Originally posted by condorx
    why dont we use gets and we use fgets?i think stdin is used for input
    because it doesn't do length checking. If the user provides a string that is too long, it will still be stored in the array, but will extend past it, overwriting memory not owned by your program.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic port scanner code .. pls help ???
    By intruder in forum C Programming
    Replies: 18
    Last Post: 03-13-2003, 08:47 AM
  2. i dont know what to do. pls. help!!!!
    By Unregistered in forum C++ Programming
    Replies: 14
    Last Post: 03-14-2002, 03:24 PM
  3. help me pls..... :(
    By mocha_frap024 in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 10:46 AM
  4. pls help me!!
    By hanseler in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 08:46 PM
  5. Pls Help Me In This Question!!!
    By Joanna in forum Windows Programming
    Replies: 1
    Last Post: 10-20-2001, 03:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21