Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't this printf() statement print two string variables in C?

I have been researching like mad while I learning C. I have been debugging a C program and I thought I had some major issues here. Now I have critical issues. I made a dummy program to print two strings in one statement as follows:

   #include<stdio.h>

int main(int argc, char* argv[])
{
    char *herp = "Derp";
    char *derp = "Herp";

    printf("Herp %s Derp %s\n", herp, derp);

    return 0;
}

This prints out as expected. I get

Herp Derp Derp Herp

So, I thought, let me debug my own program by doing something similar. The following line in my program

printf("word is: %s and jumbled word is: %s\n", word, jumbleWord);

Should print out something like

Word is: word and jumbled word is: dowr

But it prints out something like

and jumbled word is: dowr

Where did the first part of the output go? I need to be able to print these both on the same line to debug. Also, the fact that a statement like this is not working tells me that really weird things are going on and I am bald from tearing my hair out. As my linked posts indicates, I would eventually like to compare these string values, but how can I even do that if printf() is not working right?

I am posting the entire program below so you can see where everything is happening. I am just learning how to use pointers. I originally had two pointers point at the same memory when I wanted to jumble a word and that didn't work very well! So I fixed that and got two separate memory spaces with the words I need. Now, I just can't print them. This all makes sense given the code below:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX_WORD_LENGTH 25

//Define global variables 
int numWords; 

//Preprocessed Functions 
void jumblegame();
void readFile(char *[]);
void jumbleWord(char *);
void guess(char *,char *); 

int main(int argc, char* argv[])
{
    jumblegame();
    return 0;
}

void jumblegame()
{
    //Load File 
        int x = 5050; //Rows
        char *words[x];
        readFile(words);

    //Define score variables 
        int totalScore = 0;
        int currentScore = 0; 

   //Repeatedly pick a random work, randomly jumble it, and let the user guess what it is
         srand((unsigned int)time(NULL));
         int randomNum = rand() % numWords + 1;

         char source[MAX_WORD_LENGTH + 1];
         char jumble[MAX_WORD_LENGTH + 1];

         strncpy(source, words[randomNum], MAX_WORD_LENGTH + 1);
         strncpy(jumble, words[randomNum],MAX_WORD_LENGTH + 1);

         jumbleWord(jumble);

         guess(source, jumble);
         //printf("Random word is: %s\n ", words[randomNum]);
         //randomly jumble it           
}

void readFile(char *array[5049]) 
{
    char line[256]; //This is to to grab each string in the file and put it in a line. 
    int z = 0; //Indice for the array

    FILE *file;
    file = fopen("words.txt","r");

    //Check to make sure file can open 
    if(file == NULL)
    {
        printf("Error: File does not open.");
        exit(1);
    }
    //Otherwise, read file into array  
    else
    {
        while(!feof(file))//The file will loop until end of file
        {
           if((fgets(line,256,file))!= NULL)//If the line isn't empty
           {
             int len = strlen(line); 
             if (len > 0 && line[len - 1] == '\n') line[len - 1] = '\0';
             array[z] = malloc(strlen(line) + 1);
             strcpy(array[z],line);
             z++;
           }    
        }
    }
    fclose(file);
    numWords = z; 
}

void jumbleWord(char *word)
{
    int wordSize = strlen(word) - 1; 
    //durstenfeld Implementation of Fischer-Yates Shuffle
        int i; 
        int j; 
        char temp;
        for(i = wordSize - 1; i > 0; i--)
        {
            j =  rand() % (i + 1);
            temp = word[j];
            word[j] = word[i];
            word[i] = temp;
        }
}

void guess(char *word, char *jumbleWord)
{
     printf("original word is: %s\n", word);
     printf("jumbled word is: %s\n", jumbleWord);
     printf("source is: %s and jumbled word is: %s\n", word, jumbleWord);
}

I think most people at this point would burn C and slap themselves for sucking so bad at it. However, I am going to keep trucking along. So let me apologize for any derp, but please know that I have spent many hours probably being really stupid and staring at this. I would love to say, "Hey, C is so stupid because it won't do what I am telling it to". Unfortunately, I can't believe this. I think it is doing exactly what I am telling it to do, but I am too close to this problem to see what I am doing wrong.

As always, thank you for your help. My deepest respect, GeekyOmega

like image 448
GeekyOmega Avatar asked Dec 04 '22 02:12

GeekyOmega


1 Answers

You have a carriage return '\r' at the end of the word(s).

Carriage returns move the write cursor to the left of the screen, so it is writing it, but it's overwriting what was already there.

like image 185
Doug Currie Avatar answered Jan 07 '23 20:01

Doug Currie