Hi everyone,
I'm new to C programming and I'm trying to write a program that can print text in columnar format with a maximum width.
I've been trying with this but I get a seg fault. Am I going about this the right way? Any help would be really appreciated.
Thanks,
Cam
PS I don't care if you say my code sucks, as long as you tell me how I can improve it :p
#include "columnarText.h"
#include <string.h>
#include <stdio.h>
void textPrint(const char *text, const int columnWidth){
int lineLength = 0;
int count = 0;
int stop = 0;
int finish = 0;
while(stop!=1){
char *currWord;
int i=0;
int k=count;
while(strcmp(text[k]," ")!=0){
if(strcmp(text[k],"/0")==0){
finish = 1;
break;
}
currWord[i]=text[k];
k++;
i++;
count=k;
}
currWord[i]="/0";
i=0;
if((strlen(currWord)+lineLength)<column){
printf("%s",currWord);
lineLength+=strlen(currWord);
}
else{
printf("/n");
printf("%s",currWord);
lineLength = strlen(currWord);
}
if(finish==1){
stop=1;
}
}
}
int main(void)
{
char *s = "This is a test string to test my program with.";
textPrint(s,10);
return 0;
}