Thread: string

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    30

    string

    Got another prg: write a program to concatenate 2 strings. Apart from usin strcat, is there another simple way of doing it?

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    >Apart from usin strcat
    Yes, do your own version of strcat.
    Loading.....
    ( Trying to be a good C Programmer )

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    30
    My version is not working. that's why i asked for another version.

    Code:
    #include "stdio.h"
    #include "conio.h"
    
    #define N 30
    
    main()
    {
       char *str,*str1,temp;
       int i=0,j=0,k,flag;
       char x[N];
       clrscr();
    
       printf("Input a string: ");
       scanf("%s",str);
       printf ("\n");
    
       printf("Input 2nd string: ");
       scanf("%s",str1);
       printf ("\n");
    
       while(*(str+i)!='\0')
       {
    	i++;
       }
       printf("%d", i);
    
       while(*(str1+j)!='\0')
       {
    	j++;
       }
       printf("%d", j);
    
    
       while (i<=0)
       {
         x[k]=*(str+i);
         printf("%c", *(str+i));
         i++;
       }
    
       while (k<=0)
       {
         x[k]=*(str+k);
         printf("%c", *(str+k));
         k++;
       }
    
    }


  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    30
    i've changed it but still it isn't working...


    Code:
    
    k=0;
    while (i>=0 && *(str+i)!='\0')
    {
         x[k]=*(str+i);
         printf("%c", *(str+i));
         i++; 
         k++
    }

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    Code:
    while (k<=0)
       {
         x[k]=*(str+k);
         printf("%c", *(str+k));
         k++;
       }
    This one won't work either.
    You can write:
    Code:
    while( *str1 != '\0' );
    {
    x[k] = *str1++;
    printf( "%c", x[k] );
    k++;
    }
    The while statement you posted in your last post doesn't need the condition i >= 0
    Also, if you want to use the variable i, you have to initialize i to 0 ( for that loop ).

    And at the end append the null character at the end of x[]
    Loading.....
    ( Trying to be a good C Programmer )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM