2 Replies - 764 Views - Last Post: 02 September 2009 - 01:34 PM Rate Topic: -----

#1 hd_pulse   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 31-August 09

print complete string on different line

Posted 02 September 2009 - 12:48 PM

  # define A 100
# define M 100
#include<ctype.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char a[M];
clrscr();
gets(a);
int k[A][A],r=0,c=0,i,j,l;
for(i=0;i<strlen(a);i++)
{
if(a[i]!=' ')
{
k[r][c]=a[i];
c++;
}
else
{
k[r][c]='\0';
r++;
c=0;
}
}
printf("c=%d \n",c);
printf("r=%d \n",r);
for(  i=0;i<=r;i++)
{
printf("%s\n",k[i]);
}
getch();
} 


in the above program I want to print each string in different line.
say if the given string is "john is a hardworking student" I'm trying to print this way...

john
is
a
hardworking
student

but my output is coming like this...
j
i
a
h
s
i.e first character of each string in the sentence.

What is the probable error i m making????
plz I'm new to c programming help me!!!

regards

Is This A Good Question/Topic? 0
  • +

Replies To: print complete string on different line

#2 IngeniousHax   User is offline

  • |>|20-514<|{3|2

Reputation: 84
  • View blog
  • Posts: 1,385
  • Joined: 28-March 09

Re: print complete string on different line

Posted 02 September 2009 - 01:07 PM

try searching the string for a null character, such as a... space, or newline marker.

if(/*something*/ == '\0')
	printf(/*print the stuff*/)


Was This Post Helpful? 0
  • +
  • -

#3 hd_pulse   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 31-August 09

Re: print complete string on different line

Posted 02 September 2009 - 01:34 PM

i've made such little changes in the prorgam
# define A 100
# define M 100
#include<ctype.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char a[M];
clrscr();
gets(a);
int k[A][A],r=0,c=0,i,j,l;
for(i=0;i<strlen(a);i++)
{
if(a[i]!=' ')
{
k[r][c]=a[i];
c++;
}
else
{
k[r][c]='\0';
r++;
c=0;
}
}
printf("c=%d \n",c);
printf("r=%d \n",r);
/*for(  i=0;i<=r;i++)
{
printf("%s\n",k[i]);
}*/
for(  i=0;i<=r;i++)
{
for(j=0;j<=c;j++)
{
if(k[i][j]=='\0')
{
printf("here%s\n",k[i]);
}
}
}
getch();
} 


now the output is
j
i
a

for such string..."john is a hardworking student" not what I wan't to get.

Plz help anything else needs to be changed so please post !!
thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1