Need help debugging a C concatenation program

Viewed 62

I'm trying to debug some code for this program. It's supposed to read two strings and concatenate them into a new string. I've got three files that make up the program:

main.c:

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

int main(void){
  char mainStr[MAXSTR]="Initial string";
  safeReadString(mainStr,MAXSTR);
  int remChars=MAXSTR-strlen(mainStr)-1;
  int i;
  printf("Got here");
  if (remChars>0)
  {
    char * insStr;
    safeReadString(insStr,remChars);
    char newStr[strlen(mainStr)+strlen(insStr)+1];
    for(i=0;i<=strlen(mainStr);i++)
      newStr[i]=mainStr[i];
    for(int j=0;j<=strlen(insStr);j++)
      newStr[i+j]=insStr[j];
    printf("The result of concatenating:\n%s\nwith:\%s\nis:\n%s\n",mainStr,insStr,newStr);
  }
  else
    printf("No room for inserting a string\n");

}

safeStr.c:

#include "safeStr.h"

void safeReadString(char *str,int size)
{
  char c;
  char* s=str;
  int count=0;
  printf("Please type a string of at most %d characters.\n",size);

  c=getchar();
  while((c!='\0') || (count<size))
  {
    s+=c;
     s++;
     count++;
     c=getchar();
     printf("*s: %d s: %s count: %d\n", *s, s, count);
  }
  *s='\0';
}

and safeStr.h:

#include<string.h>
#include<stdio.h>
#define MAXSTR 80

void safeReadString(char *str,int size);

I'm pretty sure most of the problems are in the function, since I try to input a string and I get nothing back.

1 Answers

I fixed some bugs in the following code.

#include<string.h>
#include<stdio.h>
#define MAXSTR 80

void safeReadString(char *str,int size)
{
        char c;
        char* s=str;
        int count=0;

        rewind(stdin);
        printf("Please type a string of at most %d characters.\n",size);

        c=getchar();
        //while((c!='\0') || (count<size))
        while((c!=EOF) && (count<size))
        {
                //s+=c;
                *s = c;
                s++;
                count++;
                c=getchar();
                printf("*s: %d s: %s count: %d\n", *s, s, count);
        }
        *s='\0';
}

int main(void){
        char mainStr[MAXSTR]="Initial string";
        safeReadString(mainStr,MAXSTR);
        int remChars=MAXSTR-strlen(mainStr)-1;
        int i;
        printf("Got here");
        if (remChars>0)
        {
                //char * insStr;
                char insStr[MAXSTR];
                safeReadString(insStr,remChars);
                char newStr[strlen(mainStr)+strlen(insStr)+1];
                //for(i=0;i<=strlen(mainStr);i++)
                for(i=0;i<strlen(mainStr);i++)
                        newStr[i]=mainStr[i];
                for(int j=0;j<=strlen(insStr);j++) 
                        newStr[i+j]=insStr[j];
                printf("The result of concatenating:\n%s\nwith:\%s\nis:\n%s\n",mainStr,insStr,newStr);
        }
        else
                printf("No room for inserting a string\n");

}

when enter string. you should enter string, press enter, press Ctrl+D. and there will be '\n' left in each string out of this function.

don't use getchar(), try fgets() ~

Related