B
Ben Pfaff
[email protected] said:For ( i = 0 ; i != isspace(T1d) ; i++ )
cnt1++ ;
'for' is not capitalized (and comparing 'i' to the return value
of isspace makes little sense).
[email protected] said:For ( i = 0 ; i != isspace(T1d) ; i++ )
cnt1++ ;
[email protected] said:For ( i = 0 ; i != isspace(T1d) ; i++ )
cnt1++ ;
'for' is not capitalized (and comparing 'i' to the return value of
isspace makes little sense).
Moi said:[email protected] said:For ( i = 0 ; i != isspace(T1d) ; i++ )
cnt1++ ;
'for' is not capitalized (and comparing 'i' to the return value of
isspace makes little sense).
And: what is the intended *purpose* of this program fragment ?
Moi said:For ( i = 0 ; i != isspace(T1d) ; i++ )
cnt1++ ;
'for' is not capitalized (and comparing 'i' to the return value of
isspace makes little sense).
And: what is the intended *purpose* of this program fragment ?
I would guess that the intention is to count the number of spaces in a
string.
[email protected] said:I feel so sad of being so worthless with C at this time. At least,
it's a challenge not about what i get to learn but about how worthless
i can feel at some point...
This block is part of a small application tutorial from a website i
decided to take on. Among the question, one is to have a sentence
reversed ie : "i think yes" => "yes think i".
At the stage this application is, following the author's tutorial, he
can only expects the use of basic string functions (isspace might not
even be part of the answer), basic loops, arrays and that's about it.
It isn't yet question of pointers and functions.
My idea is to start from a sentence in memory, get the number of
spaces in it and initiate a multidimension array from it char Array[L]
[C], i should get L from the number of blank spaces returned by
isspace and C from the number of characteres (letters) between blank
spaces.
I would then print the multidimension array down from the last line
(keeping words in order) up to the first line.
Hi,
I think this code below is working both on linux and windows. A very
few minor changes were needed. Published in 1996 doesn't make this
code so "out of range".
/* Manage 10 strings in an array of pointeurs */
#include <stdio.h> /* printf, gets */
#include <string.h> /* strcmp, strcpy, strlen */
#include <stdlib.h> /* malloc, free, exit */
int main(void)
{
char *list[10] ;
char *temp ;
char buffer[128] ;
int i = 0 ;
int m, n, rep ;
printf("\n\nINITIALISATION OF A LIST\n") ;
printf("\nMaximum of 10 entries in this format :\n") ;
printf("Last name, first name (country)\n") ;
do
{
printf("\nEntry %d : ", i+1 ) ;
fgets(buffer, 128, stdin) ;
if (strcmp(buffer, "0"))
{
if ((list = (char *) malloc(strlen(buffer) + 1)) == NULL )
{
printf("\nOut of memory.\n") ;
if ( i > 0 )
{
while ( getchar() != '\n') ;
printf("\nDisplay current listing ? (o/n)") ;
if ( rep == 'o' )
break ; /* does break quit the if from if (strcmp(buffer,
"0")) ??? */
No.
}
exit(1) ;
}
strcpy(list, buffer) ;
i++ ;
}
} while ( i < 10 && strcmp(buffer, "0") ) ;
for ( m = i - 1 ; m > 0 ; m-- )
for ( n = 0 ; n < m ; n++ )
if ( strcmp(list[n], list[n+1]) > 0 ) /* The author wrote list
instead of list[n], could it had been : strcmp( *list, *(list+1) ) ???
*/
{
temp = list[n] ;
list[n] = list[n+1] ;
list[n+1] = temp ;
}
if ( i > 0 )
{
printf("\nENTRY LISTING : \n") ;
for ( m = 0 ; m < i ; m++ )
printf("%s\n", list[m] ) ;
}
for ( m = 0 ; m < i ; m++ )
free(list[m]) ;
}
Recopied from this book : PC Poche Langage C from G.Willms published
by Micro Application...hope there won't be copyright big issues...
int L = 0 ; int C = 0 ; /* Lines and Columns of T2d */
char T1d[12] = "I think yes " ;
char T2d[L][C] ; /* T2d should host T1d */
L = 0; C = 0; char T2d[L][C]; What are the dimensions of T2d?
L = 0; C = 0; char T2d[L][C]; What are the dimensions of T2d?
2 dimensions
L = 0; C = 0; char T2d[L][C]; What are the dimensions of T2d?2 dimensions
That's how MANY dimensions there are (rows and columns).
How many rows are there, and how many columns?
To make it simpler:
int foo = 0;
char bar[foo];
What is the size of bar?
Hi,
I think what is below should work with little modification.
To reply all in one earlier post about this code.
The tutorial this application is from might be light, i can't expect
much from free resources found on the internet but i think it gives a
good approach of basic C and an understanding of algorithms which is
what I'm trying to focus on now before i get to more advanced
programming techniques (pointers...) and other languages... I'd just
like to be able at this stage of learning to implement basic programms
without first using pointers.
If someone can have a quick view. The tutorial for this application
skips the answer for that specific question so i don't have any
references on what to rely. And i'd expect it's possible to revert
words (not letters) in C only using simple loops and non pointers
arrays. I spend 2-3 hours looking for a similar code on the internet
but it seems as Ben says multidimensional arrays a pain in C. While
i'm learning C (for no specific reasons), as an accountant, i'd like
to focus whenever i meet that kind of basic challenges on these since
i think it could help with excel or other spreadsheets applications.
I understand this code here is very, very weak, the sentence must be
part of the code, it then must have a blank space at the end (i
couldn't make isspace to grab each ' ' or '\0'...) and i'm not even
sure if it's the good way to start from.
I think this can be part of one or 2 functions since it performs one
main function (revert words).
In the code below, i'd like to host data from a char unidimensional
array into a 2 dimensional array.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int L = 0 ; int C = 0 ; /* Lines and Columns of T2d */
char T1d[12] = "I think yes " ;
char T2d[L][C] ; /* T2d should host T1d */
int cnt1 = 0 ; int cnt2 = 0 ; int cnt3 = 0 ;
int i = 0 ; int j = 0 ; int z = 0 ;
while (T1d )
{
if ( isspace(T1d) )
L++;
i++ ;
}
printf("\n\nPour controle : Nombre de lignes : %d \n", L-1) ; /* L
value is wrong b/c of the extra space */
for ( i = 0 ; T1d != ' ' ; i++ )
cnt1++ ;
for ( i = cnt1+1 ; T1d != ' ' ; i++ )
cnt2++ ;
for ( i = cnt1+cnt2+2 ; T1d != ' ' ; i++ )
cnt3++ ;
printf("\nPour controle : nbr de lettres : %d %d %d\n", cnt1, cnt2,
cnt3) ;
C = cnt1 + cnt2 + cnt3 ;
printf("\nPour controle : nbr de colonnes : %d \n\n", C) ;
for ( i = L ; i <= 0 ; i-- )
for ( j = C, z = 12 ; j <= 0 ; j--, z-- )
T2d[j] = T1d[z] ;
for ( i = L ; i = 0 ; i-- )
for ( j = 0 ; j <= C ; j++ )
printf("%s", (T2d[j])) ;
Barry Schwarz said:On Fri, 24 Jul 2009 10:08:18 -0700 (PDT), "(e-mail address removed)"
int L = 0 ; int C = 0 ; /* Lines and Columns of T2d */
Would you please fix the line length in your newsreader so that you
don't get a 2 for 1 expansion. I have cleaned up the ones that
follow.
char T1d[12] = "I think yes " ;
char T2d[L][C] ; /* T2d should host T1d */
In C90, this is invalid syntax. In C99 it is a constraint violation.
Even if you compiler accepts it, T2D has 0 elements.
[...][email protected] said:L = 0; C = 0; char T2d[L][C]; What are the dimensions of T2d?2 dimensions
That's how MANY dimensions there are (rows and columns).
How many rows are there, and how many columns?
<>
L = 3 ... blank space
C = 5 ... the number of letters in the longest word eg think = 5.
To make it simpler:
int foo = 0;
char bar[foo];
What is the size of bar?
Size of bar should be 2 bits = "" + '\0' .
Keith Thompson said:char empty_string = "";
The size of empty_string is 1 byte. It contains a single char with
the value '\0'. It happens to contain a string of length 0.
Phil Carmody said:Keith Thompson said:char empty_string = "";
The size of empty_string is 1 byte. It contains a single char with
the value '\0'. It happens to contain a string of length 0.
Presumably missing []?
The value of rep is indeterminate so this invokes undefined behavior.
break ; /* does break quit the if from if (strcmp(buffer,
"0")) ??? */
No.
}
exit(1) ;
This is not a portable value to return from main.
}
strcpy(list, buffer) ;
i++ ;
}
} while ( i < 10 && strcmp(buffer, "0") ) ;
for ( m = i - 1 ; m > 0 ; m-- )for ( n = 0 ; n < m ; n++ )
if ( strcmp(list[n], list[n+1]) > 0 ) /* The author wrote list
instead of list[n], could it had been : strcmp( *list, *(list+1) ) ???
While those are syntactically correct they represent different logic.
If he had written *(list+n) and*(list+n+1), it would mean the same as
what your wrote. The expression list as an argument to strcmp without
some kind of dereference operator is a constraint violation.
*/
{
temp = list[n] ;
list[n] = list[n+1] ;
list[n+1] = temp ;
}if ( i > 0 )
{
printf("\nENTRY LISTING : \n") ;
for ( m = 0 ; m < i ; m++ )
printf("%s\n", list[m] ) ;
}for ( m = 0 ; m < i ; m++ )
free(list[m]) ;
A return with either 0 or EXIT_SUCCESS would be nice here.
Are you sure you copied it correctly?
Ha! I nearly did use char for that one, and changed my mind at the
last moment because I guessed (wrongly, it seems) that I'd draw
flak for breaking consistency with other character handling
routines.
[snip]
Er, kindasorta, yes. Anyway, I always use int for Booleans.
[snip]
I see your point - I had forgotten that fgets takes an int, not a
size_t - and I half-agree with it. But only half.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.