Thread: Change variable value

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    3

    Change variable value

    Code:
    #include <stdio.h>
    int main()
    {
        char stem[1];
        
        printf("Type in the infinitive form:\n\n");
        scanf("%s", stem);
        
        printf("Present Tense\nInfinitive: %s\n\n", stem);
            printf("First Person Singular: %s\n", stem);
                printf("Second Person Singular: %s\n", stem);
                    printf("Third Person Singular: %s\n", stem);
                    
            printf("First Person Plural: %s\n", stem);
                printf("Second Person Plural: %s\n", stem);
                    printf("Third Person Plural: %s\n", stem);
        
        printf("Present Passive: %s\n", stem);
        printf("Negative Passive: %s\n", stem);
    }
    I'm trying to make a verb conjugator in C. The user will attribute a value (a verb) to the variable "stem" and I want this variable to be displayed in the printf's with the last 3 letters removed so I can individually add the pronominal endings.

    So if the value "abcdefg" is attributed to "stem", I want it to be displayed as just "abcd" in the printf's below the scanf but I got no clue how to do this. This is not a thing apparently:

    Code:
    printf("First Person Singular: %s\b\b\b", stem);

    I'm really in the beginning still so any ideas on how to do this? Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,526
    > char stem[1];
    I guess the first thing you need to do is allocate enough space in your stem string to actually store a string.
    Given that a string needs at least a \0, an array with only 1 element isn't that useful.

    Knowing the length of the string is useful, so let's start with
    int p = strlen(stem);

    It's useful to note that stem[p] is where the \0 character is at the end of the string.

    Perhaps you could then do something like
    stem[p-3] = '\0';
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable change query
    By totallyclueless in forum C Programming
    Replies: 5
    Last Post: 10-06-2014, 08:26 AM
  2. Program to change the name of a variable
    By acho.arnold in forum C Programming
    Replies: 6
    Last Post: 06-15-2013, 03:31 AM
  3. How to know when a value of a variable change?
    By Hannibal2010 in forum C Programming
    Replies: 12
    Last Post: 11-25-2011, 02:16 PM
  4. Doesn't consider change in variable
    By f0xy in forum C Programming
    Replies: 5
    Last Post: 10-29-2011, 08:33 AM
  5. unwanted Variable Change
    By purebuu in forum C Programming
    Replies: 3
    Last Post: 02-21-2006, 08:56 PM

Tags for this Thread