Thread: int char crap char I cannot get rid of it.

  1. #1
    Banned
    Join Date
    Aug 2017
    Posts
    861

    int char crap char I cannot get rid of it.

    I'm tired of playing with this. I did this both ways I figured out how to do this, and I still cannot figure out how to get some crap char out of the substring, or its a \n or something that is putting in a end line between the substring and main string.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    #define MAX_LEN 256
    
    int main(void)
    {
        char string1[MAX_LEN]; // main string
        char string2[MAX_LEN]; // sub string
        char string3[MAX_LEN]; // holding string
        int position=0,i=0;
        int ss=0;
        int x,tsz,op;
        int srt1_len, srt2_len;
         
     
        printf("Enter First String: ");
        fgets(string1, MAX_LEN, stdin);
        printf("Enter Second String: ");
        fgets(string2, MAX_LEN, stdin);
        printf("Enter the insert position: ");
        scanf("%d",&position);
        srt1_len = strlen(string1);
        srt2_len = strlen(string2);
        //printf("stLen 1 %d : stLen 2 %d\n", srt1_len, srt2_len);
        i=0;
     
     // Copying the first string into another array
        while(i <= srt1_len)
        {
            string3[i]=string1[i];
        //    printf("string3[i %c], string1[i %c]\n", string3[i], string1[i]);
            i++;
        }    
        tsz = srt2_len+srt1_len;
        op = position+srt2_len;
        
        if (string2[srt2_len ] == '\n')
            string2[srt2_len ] = '\0';
     printf("Start position %d\nsrt2_len %d", position ,srt2_len);
     printf("Start op %d\n", op);
     // Adding the sub-string
        for(i=position;i < op;i++)
        { printf(" i = %d tsz %d\n", i, tsz);
            
        //    x = string3[i];
        //    printf("x = %c\n i %d\n", x, i);
        //    printf("top of if\n");
         
            if(ss < srt2_len)
            { printf("ss at start of loop %d\n\n", ss);
             
                
                    
                if (string2[ss - 1] == '\n')
                    string2[ss - 1] = '\0';
                string1[i] = string2[ss];  
    
                printf("i %d string1[i %c] = ss %d string2[ss %c]\n",i ,  string1[i], ss, string2[ss]);
                ss += 1;
                
                
            }
             printf("outside bottom of if op %d \n", op);
             printf("outside bottom of if tsz %d \n", tsz);
             
        //    string1[op]=x; 
        //    if (string1[op - 1 ] == '\n')
        //        string1[op - 1 ] = '\0';
            // output start point to length of = letter from
                            // copied main string holding char[size] array
                            // putting back what is left of string one at
                            // end of substring over write 
        //    printf("string1[op %c ] = x %c\n", string1[op], x);
        //    printf("string1 %s\n", string1);
        //    op += 1;
        //    printf("op = %d\n",op);
        }
        
        for ( int j = i; j < tsz; j++)
        {
            string1[j] = string3[position];
            position++;
        }
     
        printf("%s", string1);
        
    return 0;
    }
    results,
    Code:
    // input
    $ ./insert_string_3
    Enter First String: MAIN STRING
    Enter Second String: substring
    Enter the insert position: 5
    
    // output
    
    MAIN substring
    STRING
    if you do run this, comment out the third loop, then uncommnent that code inside of the second loop and what printf you want to see what is going on. towards the end of the run you're going to start seeing. stuff like this
    Code:
    x = 
     i 21
    top of if
    outside bottom of if op 31 
    outside bottom of if tsz 22 
    string1[op  ] = x 
    but with them little squares in the output that have them four really little number or whateer they are in them.
    oh yeah and comment this out too
    Code:
     if (string2[srt2_len ] == '\n')    string2[srt2_len ] = '\0';
    it's up top
    Last edited by userxbw; 11-03-2017 at 02:02 PM.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Can you take a photo (or scan) of the piece of paper where you've drawn little boxes and stuff (representing arrays and elements) that shows how the program works?

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Hodor View Post
    Can you take a photo (or scan) of the piece of paper where you've drawn little boxes and stuff (representing arrays and elements) that shows how the program works?
    got some graph paper handy? I got a few free minutes. I do not know how well this is going to format in here.
    Code:
    | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
     M   A  I    N        S  T   R  I    N   G
                        s   u   b   s   t   r    i    n    g
    that is all I used.

    make copy of main string, keep it handy.

    then overwrite the original one using the substring one in a loop starting at position to insert. up to insert_total = insert_position + substring_len

    then put that orginal into another loop and just pick up where you left off using the insert_total as your start point, because that is the number you left off at. then just use your temp array and set it to that postion in its array, and keep the orginal array where it left off at. then just copy
    what is left in the temp array. to total length of main_string + substring. that simple.

    requires three loops max. three char arrays, 2 eggs and a bottle of milk. some int's and a typewriter. the code it already there, with printf in it even .

    simplified version
    Code:
        srt1_len = strlen(string1);
        srt2_len = strlen(string2);
        tsz = srt2_len+srt1_len;
        tpos = pos+srt2_len;
        
        while(i <= srt1_len)
        {
            string3[i]=string1[i];
            i++;
        }    
        for( i = pos; i < tpos;i++)
        {
            string1[i] = string2[ss];  
            ss += 1;
        }
        for ( int j = i; j < tsz; j++)
        {
            string1[j] = string3[pos];
            pos++;
        }
    you should practice reading code and figuring things out. what if your first job is to debug someone else's code?
    and pick up on things you might not have ever tough about on how to do something you're already doing. but theirs is better
    because they are more experienced then the reader.
    Last edited by userxbw; 11-03-2017 at 03:22 PM.

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    I would remove the trailing \n after getting the input and then proceed with the copying.
    Code:
    // remove trailing \n if any
      char *pos = strrchr(string1, '\n');
      if (pos)
        *pos = '\0';
    
    
      pos = strrchr(string2, '\n');
      if (pos)
        *pos = '\0';
    One complete way to do it:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    
    #define MAX_LEN 256
    
    
    int main(void)
    {
      char string1[MAX_LEN] = {0}; // main string
      char string2[MAX_LEN] = {0}; // sub string
      char string3[MAX_LEN] = {0}; // holding string
      char tmp[MAX_LEN] = {0};
      int position = 0, i = 0;
     
      printf("Enter First String: ");
      fgets(string1, MAX_LEN, stdin);
      printf("Enter Second String: ");
      fgets(string2, MAX_LEN, stdin);
      printf("Enter the insert position: ");
      scanf("%d", &position);
    
    
      // remove trailing \n if any
      char *pos = strrchr(string1, '\n');
      if (pos)
        *pos = '\0';
    
    
      pos = strrchr(string2, '\n');
      if (pos)
        *pos = '\0';
    
    
      for (i = 0; i < position; ++i)
        string3[i] = string1[i];
      strcat(string3, string2);
      strcpy(tmp, string1 + position);
      strcat(string3, tmp);
      printf("\nNew string %s", string3);
    
    
      return 0;
    }
    Output:
    Enter First String: MAIN_STRING
    Enter Second String: substring
    Enter the insert position: 5


    New string MAIN_substringSTRING

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by OldGuy2 View Post
    I would remove the trailing \n after getting the input and then proceed with the copying.
    Code:
      for (i = 0; i < position; ++i)
        string3[i] = string1[i];
      strcat(string3, string2);
      strcpy(tmp, string1 + position);
      strcat(string3, tmp);
      printf("\nNew string %s", string3);
    
    
      return 0;
    }
    Output:
    Enter First String: MAIN_STRING
    Enter Second String: substring
    Enter the insert position: 5


    New string MAIN_substringSTRING
    this is just adding that get rid of \n code.results.
    Code:
    userx@slackwhere:~/bin
    $ ./insert_string_4
    Enter First String: MAIN STRING
    Enter Second String: substring
    Enter the insert position: 5
    MAIN substringSTRING
    all of that time waisted trying to figure it out somewhere else in the code using a different method even.
    thanks,

    so this here
    Code:
      for (i = 0; i < position; ++i)
        string3[i] = string1[i];
      strcat(string3, string2);
      strcpy(tmp, string1 + position);
      strcat(string3, tmp);
      printf("\nNew string %s", string3);
    I am going to run that to see what I see. that bold one I do not understand.
    Code:
     char *strcpy(char *dest, const char *src);
    
    how does slipping in a number with the char in the second pram work?

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Code:
    strcpy(tmp, string1 + position);

    Simply Pointer Arithmetic.



    string1 is a array of characters.
    If you use the name of a array without index, it returns the address to the first member of that array.
    In this example, the address of the first character.
    If you add a integer to a pointer, you became a pointer to that member.
    Code:
    string[20] = "Hello World!";
    printf("%s", string + 6); // outputs 'World!'
    Other have classes, we are class

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by WoodSTokk View Post
    Code:
    strcpy(tmp, string1 + position);

    Simply Pointer Arithmetic.



    string1 is a array of characters.
    If you use the name of a array without index, it returns the address to the first member of that array.
    In this example, the address of the first character.
    If you add a integer to a pointer, you became a pointer to that member.
    Code:
    string[20] = "Hello World!";
    printf("%s", string + 6); // outputs 'World!'
    ok so it just takes the array of what name it is, then the number controls the start point. Then it uses whatever is left inside of it.

    why can't people just say that? it's called plain english.
    Thnaks.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    why can't people just say that? it's called plain english.
    Thnaks.
    What language is Thnaks ?

  9. #9
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by ddutch View Post
    What language is Thnaks ?
    that is my keyboard moving the keys on me when i am not looking.

    "scientific" reason.

    fingers get out of sync with brain
    brain knows what letters, in what order, but by the time that information gets to the fingers somewhere somehow in that buffer it gets the letters mixed up on printout.

    Layman terms, it's called a "typo"
    Last edited by userxbw; 11-04-2017 at 09:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-25-2014, 06:12 AM
  2. Replies: 2
    Last Post: 09-25-2014, 04:03 AM
  3. Replies: 4
    Last Post: 07-24-2012, 10:41 AM
  4. undefined reference to `RunSwmmDll(char*, char*, char*)'
    By amkabaasha in forum C++ Programming
    Replies: 1
    Last Post: 10-31-2011, 12:33 PM
  5. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM

Tags for this Thread