#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    C programming (help for string problem)


    Hello, I am a beginner in C programming.
    I need help with tasks related to the string.
    "Write a program that the string with s[200000], to add to a string x, which is a letter of the alphabet."

    I tried all the ways but not asleep, because when I use strcat (s, x), there is a problem: the string: "alexa" string to "p" strcat (s, x), and then the string looks like this "plexa" or something like that...
    I think I need to put a string in the middle of a string a new string and just move it, depending on whether the string to be placed in front of or behind a string?

    CAN SOMEONE HELP ME?
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,216
    Rep Power
    2222
    Show us the code, because your narrative does not make sense. Show us the code fragment that declares s and x, sets the values of s and of x, calls strcat and then the printf that displays the resultant concatenated string.

    We've used strcat many many time with consistent results. You're doing something wrong, but we can't know what that something wrong is until you show us the code.

    And please use code tags.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    unsigned int N; //n strlen of string
    char s[200000];
    char x[1]; //is a letter of alphabet


    void strcatXandS(void){
    strcat(s,x);

    }

    int main(){
    int i;
    scanf("%u",&N);
    for(i=0;i<N;i++){
    s[i]=getchar();
    }
    strcatXandS();
    putchar(strcat(s,x));
    return 0;
    }
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,216
    Rep Power
    2222
    Use code tags. And also indent your code, especially if you're going to use a fracked up style like K&R that absolutely requires consistent indenting.

    Your code, using code tags and formatted properly with Allman style:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    unsigned int N; //n strlen of string
    char s[200000];
    char x[1]; //is a letter of alphabet
    
    
    void strcatXandS(void)
    {
        strcat(s,x);
    }
    
    int main()
    {
        int i;
        scanf("%u",&N);
        for(i=0;i<N;i++)
        {
            s[i]=getchar();
        }
        strcatXandS();
        putchar(s[i]);
        return 0;
    }
    First, you need to understand the difference between a string and a char, because your declaration of x is just plain wrong.

    Since there is not such thing as a built-in string type, C uses char arrays which contains a string of chars -- and this is the important part -- which is terminated by a null-terminator, a char with the ASCII value of zero (written '\0'). Whenever you declare a char array, you need to declare it to be one char larger in order to accommodate the null-terminator. A string one character long therefore must be declared with a size of two; there cannot possibly be such a thing as string declared as char [1] except for an empty string.

    A char is a single value and is stored in a char variable, not in an array.

    strcat takes two char pointer arguments, which means that it takes two strings, not a string and a char. Therefore x does need to be a string, but it must be declared properly. The way you have it now, any use of x will cause buffer overflow which leads to all kinds of serious problems.

    Also, you never initialize x, so what do you think that you are concatenating onto s? Just by dumb luck, you declared x globally rather than locally, so it was initialized to zero, which is to say as an empty string. So the concatenated string would end up the same as the original.

    And when you input each character of s, you never terminate s with a null-terminator! Again, out of pure dumb luck you had declared it globally, which filled it with zeros (ie, with '\0', null-terminators), so you automatically terminated it. Now, try declaring those two arrays locally and see what kind of hell breaks loose!

    And what's with that putchar(s[i]);? You apparently want to output the single character that you had concatenated, but since x is an empty string, s[i] should still be '\0', which is not a displayable character.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0
    can you write me a simple function of strcat(s1,s2) where they will be placed in one string , but to be shown as s1?
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,216
    Rep Power
    2222
    I will only modify your own code to illustrate corrections:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    unsigned int N; //n strlen of string
    char s[200000];
    char x[] = "p"; //is a letter of alphabet -- this initialized x to the string, "p", and 
                       //  leaves it up to the compiler to allocate enough space to hold it (two characters)
    
    // OK as it stands
    void strcatXandS(void)
    {
        strcat(s,x);
    }
    
    int main()
    {
        int i;
        scanf("%u",&N);
        for(i=0;i<N;i++)
        {
            s[i]=getchar();
        }
    
        // now that you have input the string, you need to add the null-terminator
        //  upon leaving the for-loop, i points to the next location to receive a char
        s[i] = '\0';
    
        // now s has been properly terminated and can be used
        // string functions such as strcat absolutely depend on that null-terminator 
        //      being there; it is that vitally important!
    
        strcatXandS();
        putchar(s[i]); // this will output 'p'.  I would prefer printf("%s\n", s); to output the entire string
    
        return 0;
    }
    I can think of two other approaches to concatenating a char to a string:

    1. Convert the char to a string and then strcat; eg
    Code:
        char x[2];
        char  ch;
    
        ch = 'p';  // not the single quotes for a char, as opposed to double quotes for a string
        sprintf(x, "%c", ch);
        strcat(s, x);
    2. Do all the work manually. Find the end of the string in s, overwrite its null-terminator with the new char, and place a new null-terminator after that new char; eg:
    Code:
        char  ch;
        int   len;
       
        ch = 'p';
        len = strlen(s);   // the number of chars in s, minus the null-terminator
                            // like the other string functions, this depends absolutely on the null-terminator
        s[len] = ch;  // len is the index to s' null-terminator
        s[len+1] = '\0';  // always follow with the null-terminator
    This second method could also be used in the first method to replace the sprintf. There's more than one way to do it.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0
    Thanks Man! :)

IMN logo majestic logo threadwatch logo seochat tools logo