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;
}
while(strcmp(text[k]," ")!=0){
		if(strcmp(text[k],"/0")==0){

strcmp() is prototyped as: int strcmp ( const char * str1, const char * str2 ); The first argument passed to it must be a string. text[k] is a character and not a string.
Haven't look at your program logic.


[edit:] currWord[i]=text[k]; That will not work neither. currWord is a pointer to char not an array of pointers to char

you could, of course, just use the format modifiers that go with printf()

ex. printf("%20s",myText); prints the char string "myText" in a 20-character wide field, left justified, padding unused characters with whitespace.

while printf("%-20s",myText); does the same thing except the text is right-justified.

you are responsible for ensuring that the length of "myText" does not exceed the length of the specified field.


.

>you could, of course, just use the format modifiers that go with printf()
Sensitive advise.
Examples

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.