Okay. After having completely rewritten this program over in favor of fgets, it mostly reads in the words.
This comes in two pieces, compiled upon a gcc linux machine. However, a redirect statement is required for this.
So once compiled it must be run like this:
./a.out < data1
So here's "data1":
S T E L B M T F E Y D E E P S R T C I A E E
N N E L I T R L B D E T A R E M U N E T Y L
N O I T A N I M I R C N I F L E S S E N T A
A U I D E W A R R A N T N U P R U S S E R P
P G S G E A L P A P B A N P S A S S N M E A
C O N S I T U T I O N D E E C W S O O H P D
S V W D E L A N E E J A M E S M A D I S O N
A E D E S N E G R J C U L T N O H L T I R A
A R C E R R T R E E S B O N E E I D N N P R
S N J U D I C I A L A S S E C O R P E U D I
S M R A R A E B W B E S S M E O A U V P E M
O E O I A I L N O U C D O D S S E N N I G R
L N I D G Y T R C O M P E N S A T I O N N D
D T O Z E H P Y N D R L E E A O H S C O I B
I T P S U E T G O L U Z M M R B E H P I R T
E O I E A R R S U U I B H A Y L L M S T F A
R I N R E E E F U T L V Q U A R T E R I N G
S I D B S R R D I Y E N I G M I A N A T I R
S Q I S E B S C N S P E E C H R O T A E Y N
D L C M I L I T I A F L R N C A T S S P S E
R U T E D Y L E B I L C O H M L E T E S Y Y
L S T R T E W Z L I O S A E N S A E I Y A L
AMENDMENT
ASSEMBLY
BAIL
BEARARMS
CITIZEN
CIVIL
COMPENSATION
CONGRESS
CONSITUTION
CONVENTIONS
DELEGATED
DOUBLEJEOPARDY
DUEPROCESS
ENUMERATED
FREEDOM
GOVERNMENT
ILLEGAL
INDICT
INFRINGED
JAMESMADISON
JUDICIAL
LAWSUIT
LIBEL
LIBERTY
LIFE
MILITIA
MIRANDA
NECESSARY
PEACEABLY
PEERS
PETITION
POWER
PRESS
PROBABLECAUSE
PROPERTY
PUNISHMENTS
QUARTERING
RELIGION
RIGHTS
SEARCH
SECURITY
SEIZURE
SELFINCRIMINATION
SLANDER
SOLDIERS
SPEECH
SPEEDY
TRIAL
UNREASONABLE
WARRANT
WITNESS
Here's the program itself:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROWS 50
#define COLUMNS 50
#define TR 100
#define TC 100
int main( )
{
int s1 = 0, s2 = 0;
int sizeOfGrid = 0;
int wordCount = 0;
int rowNumber = 0;
int colNumber = 0;
/* */
char tempLetters[ROWS][COLUMNS];
char letters[ROWS][COLUMNS];
char words[ROWS][COLUMNS];
char wordTemp[50];
char firstRow[100], nextRow[100];
/* fgets(stdin); */
char firstRowTemp[100], nextRowTemp[100];
/* output input */
setbuf(stdout, NULL);
for (s2 = 0; s2 < 100; s2++)
{
firstRowTemp[s2] = ' ';
nextRowTemp[s2] = ' ';
if (s2 < 50)
{
wordTemp[s2] = ' ';
for (s1 = 0; s1 < 50; s1++)
{
words[s2][s1] = ' ';
}
}
}
/* Let's attempt to despace the first line, and then count the chars. */
fgets(firstRow,100,stdin);
s1 = 0;
s2 = 0;
for (colNumber = 0; colNumber < strlen(firstRow); colNumber++)
{
if ((firstRow[colNumber] != ' ') && (firstRow[colNumber] != '\n'))
{
firstRowTemp[s1] = firstRow[colNumber];
letters[rowNumber][s2++] = firstRowTemp[s1++];
sizeOfGrid++;
}
}
/* We have now successfully gotten the size of the grid, so lets
* put it to good use within our next loops to read in the rest
* of the lines.
* We take it in a for loop up to sizeOfGrid*2 because right now,
* we still have spaces. Once we get rid of those, it'll be able to utilize
* normal sizeOfGrid count.*/
for (rowNumber = 1; rowNumber < sizeOfGrid*2; rowNumber++)
{
fgets(nextRow, 100, stdin);
s1 = 0;
s2 = 0;
for (colNumber = 0; colNumber < sizeOfGrid*2; colNumber++)
{
if ((nextRow[colNumber] != ' ') && (tempLetters[rowNumber][colNumber] != '\n'))
{
nextRowTemp[s1] = nextRow[colNumber];
letters[rowNumber][s2++] = nextRowTemp[s1++];
}
}
}
/* Next up, it's time to store the words. Scan the rest of the file for words
* and store them as chars. */
rowNumber = 0;
colNumber = 0;
while(fgets(wordTemp,50,stdin) != NULL)
{
for (s1 = 0; s1 < strlen(wordTemp); s1++)
{
if (wordTemp[s1] != '\n')
{
words[rowNumber][colNumber++] = wordTemp[s1];
}
}
colNumber = 0;
wordCount++;
rowNumber++;
}
/* For testing purposes and making sure it receives the grid entirely.*/
for (s2 = 0; s2 < sizeOfGrid; s2++)
{
printf("%c",firstRowTemp[s2]);
}
printf("\nSize of grid is %d\n",sizeOfGrid);
printf("\n");
for (rowNumber = 0; rowNumber < sizeOfGrid; rowNumber++)
{
for (colNumber = 0; colNumber < sizeOfGrid; colNumber++)
{
printf("%c", letters[rowNumber][colNumber]);
}
printf("\n");
}
for (rowNumber = 0; rowNumber < ROWS; rowNumber++)
{
for (colNumber = 0; colNumber < COLUMNS; colNumber++)
{
if ((words[rowNumber][colNumber] != ' ') && (words[rowNumber][colNumber] != '\n'))
printf("%c", words[rowNumber][colNumber]);
else
break;
}
if (colNumber > 1)
printf("\n");
}
/* End print debugging here.*/
return 0;
}
And now for my question itself...
It appears fgets is skipping over a large number of words, starting only at LIBEL instead of running through the top of the list (AMENDMENTS in this case). Where is it going wrong?
In the grid you've shown us, there are spaces between letters horizontally, but no blank lines between the lines of letters, so you need to use sizeOfGrid*2 to loop over the columns, but not the rows.
Related
I've been trying to make a calculator in C and I am having some trouble cause when I said I wanted to choose again the program ends, or sometimes it works.
I tried this:
#include <stdio.h>
int main(void)
{
int xx;
int Y, N;
int a, b, c, d;
int aa, bb, Sum, ee, ff, Multi;
float cc, dd, Sub, gg, hh, Divi;
printf("\n\nC a l c u l a t o r\n\n");
printf("C h o o s e :\n\n");
printf(" S u m ( 1 ) \t");
printf(" S u b t r a c t i o n ( 2 )\n\n");
printf(" M u l t i p l i c a t i o n ( 3 ) \t");
printf(" D i v i s i o n ( 4 )\n\n");
scanf("%d", &xx);
a = 1;
b = 2;
c = 3;
d = 4;
Y = 10;
N = 20;
if (a == xx)
{
printf("\n\nE n t e r F i r s t N u m b e r :\n");
scanf("%d", &aa);
printf("\n\nE n t e r S e c o n d N u m b e r :\n");
scanf("%d", &bb);
Sum = aa + bb;
printf("%d + %d = %d", aa, bb, Sum);
xx = xx - 1;
printf("\n\nC h o o s e a g a i n ?\n");
printf(" ( Y e s = = S a y 1 0 !)\n");
printf(" ( N o = = S a y 2 0 !)\n");
scanf("%d", &xx);
if (Y == xx)
{
printf("\n\nC h o o s e :\n\n");
printf(" S u m ( 1 ) \t");
printf(" S u b t r a c t i o n ( 2 )\n\n");
printf(" M u l t i p l i c a t i o n ( 3 ) \t");
printf(" D i v i s i o n ( 4 )\n\n");
scanf("%d", &xx);
}
if (N == xx)
{
printf("\n\n F I N\n\n");
}
}
if (b == xx)
{
printf("\n\nE n t e r F i r s t N u m b e r :\n");
scanf("%f", &cc);
printf("\n\nE n t e r S e c o n d N u m b e r :\n");
scanf("%f", &dd);
Sub = cc - dd;
printf("%f - %f = %f", cc, dd, Sub);
xx = xx - 2;
printf("\n\nC h o o s e a g a i n ?\n");
printf(" ( Y e s = = S a y 1 0 !)\n");
printf(" ( N o = = S a y 2 0 !)\n");
scanf("%d", &xx);
if (Y == xx)
{
printf("\n\nC h o o s e :\n\n");
printf(" S u m ( 1 ) \t");
printf(" S u b t r a c t i o n ( 2 )\n\n");
printf(" M u l t i p l i c a t i o n ( 3 ) \t");
printf(" D i v i s i o n ( 4 )\n\n");
scanf("%d", &xx);
}
if (N == xx)
{
printf("\n\n F I N\n\n");
}
}
if (c == xx)
{
printf("\n\nE n t e r F i r s t N u m b e r :\n");
scanf("%d", &ee);
printf("\n\nE n t e r S e c o n d N u m b e r :\n");
scanf("%d", &ff);
Multi = ee * ff;
printf("%d x %d = %d", ee, ff, Multi);
xx = xx - 3;
printf("\n\nC h o o s e a g a i n ?\n");
printf(" ( Y e s = = S a y 1 0 !)\n");
printf(" ( N o = = S a y 2 0 !)\n");
scanf("%d", &xx);
if (Y == xx)
{
printf("\n\nC h o o s e :\n\n");
printf(" S u m ( 1 ) \t");
printf(" S u b t r a c t i o n ( 2 )\n\n");
printf(" M u l t i p l i c a t i o n ( 3 ) \t");
printf(" D i v i s i o n ( 4 )\n\n");
scanf("%d", &xx);
}
if (N == xx)
{
printf("\n\n F I N\n\n");
}
}
if (d == xx)
{
printf("\n\nE n t e r F i r s t N u m b e r :\n");
scanf("%f", &gg);
printf("\n\nE n t e r S e c o n d N u m b e r :\n");
scanf("%f", &hh);
Divi = gg / hh;
printf("%f / %f = %f", gg, hh, Divi);
xx = xx - 4;
printf("\n\nC h o o s e a g a i n ?\n");
printf(" ( Y e s = = S a y 1 0 !)\n");
printf(" ( N o = = S a y 2 0 !)\n");
scanf("%d", &xx);
if (Y == xx)
{
printf("\n\nC h o o s e :\n\n");
printf(" S u m ( 1 ) \t");
printf(" S u b t r a c t i o n ( 2 )\n\n");
printf(" M u l t i p l i c a t i o n ( 3 ) \t");
printf(" D i v i s i o n ( 4 )\n\n");
scanf("%d", &xx);
}
if (N == xx)
{
printf("\n\n F I N\n\n");
}
}
}
I've tried a lot of times trying to fix the code but it simply doesn't.
You have a lot of variables and repetition of code for no reason. I realize that my answer behaves differently than original question but looking at the code I don't think you understand what you really want. Start with this:
#include <stdio.h>
enum {
ADD = 1,
SUBTRACT,
MULTIPLY,
DIVIDE,
YES = 10,
NO = 20,
};
int main(void) {
int choice;
double values[2];
for(;;) {
printf(
"\n\nC a l c u l a t o r\n\n"
"C h o o s e :\n\n"
" S u m ( 1 ) \t"
" S u b t r a c t i o n ( 2 )\n\n"
" M u l t i p l i c a t i o n ( 3 ) \t"
" D i v i s i o n ( 4 )\n\n"
);
if(scanf(" %d", &choice) != 1) {
printf("scanf failed\n");
return 1;
}
printf("\n\nE n t e r F i r s t N u m b e r :\n");
if(scanf("%lf", &values[0]) != 1) {
printf("scanf failed\n");
return 1;
}
printf("\n\nE n t e r S e c o n d N u m b e r :\n");
if(scanf("%lf", &values[1]) != 1) {
printf("scanf failed\n");
return 1;
}
if (choice == ADD) {
double result = values[0] + values[1];
printf("%g + %g = %g", values[0], values[1], result);
} else if(choice == SUBTRACT) {
double result = values[0] - values[1];
printf("%g - %g = %g", values[0], values[1], result);
} else if(choice == MULTIPLY) {
double result = values[0] * values[1];
printf("%g * %g = %g", values[0], values[1], result);
} else if(choice == DIVIDE) {
if(values[1] == 0) {
printf("Cannot divide by zero\n");
} else {
double result = values[0] / values[1];
printf("%g / %g = %g", values[0], values[1], result);
}
}
printf("\n\nC h o o s e a g a i n ?\n");
printf(" ( Y e s = = S a y 1 0 !)\n");
printf(" ( N o = = S a y 2 0 !)\n");
if(scanf("%d", &choice) != 1) {
printf("scanf failed\n");
return 1;
}
if(choice == NO)
break;
}
printf("\n\n F I N\n\n");
}
The space between letters makes the prompt unnecessarily hard to read.
Consider changing the values you accept to be '+', '-', '*', '/' for the operators and 'y' and 'n' for the repeat prompts. This would be much easier on for the user of your program.
Should something different happen if either the first or 2nd choice is wrong? For instance, you could loop the prompt till you get a valid choice.
i wrote a code and i dont know how to continue from here .. i want to build an abc pyramide from start to back (a ,aba,abcba..etc) for example
a
a b a
a b c b a
a b c d c b a
etc..
this is my code , thanks :)
void aba(int lines)
{
//int i = 97;
char a = 97;
for (int i = 97;i <= lines + 97;i++)
{
printf("%c",i);
for (int j = 97;j <= lines + 97;j++)
{
if (j == i)
break;
printf("%c%c", j,i);
}
printf("\n");
}
void main()
{
aba(3);
}
If you have problems solving a given task always try to split it into smaller jobs:
Start by printing an a for every line of your pyramid:
void aba(int lines) {
for (int i = 0; i < lines; i++) {
printf("%c\n", a);
}
}
Now you want to have a pyramid with one character in the first line, 3 in the second, 5 in the third and so on. You can produce this sequence using (i * 2) + 1:
void aba(int lines) {
for (int i = 0; i < lines; i++) {
for (int j = 0; j < (i * 2) + 1 ; j++) {
printf("%c", a);
}
printf("\n");
}
}
The last step would be to increase the character in the first half of the line and decrease it in the last half:
void aba(int lines) {
for (int i = 0; i < lines; i++) {
char a = 'a';
for (int j = 0; j < (i * 2) + 1 ; j++) {
printf("%c", a);
if (j <= i - 1)
a += 1;
else
a -= 1;
}
printf("\n");
}
}
As it is homework, I give you a hint, not the solution:
You should observe the pattern: All lines start with a.
When you numerate the lines from top to bottom starting with 0, the line 0 has an a and 0 additional characters.
line 1 starts with the a and has 1 more upcounting character (b) and then 1 downcounting character (a).
line 2 starts with the a and has 2 more upcounting character (b c) and then 2 downcounting character (b a).
...
So, line i starts with the a and has i more upcounting characters (b c d ....) and then i downcounting characters (... c b a).
Now you have to translate that into code.
void aba(int n)
{
for (int i = 0; i < n; i++)
{
//print here what all lines have in common
for (int j = 1; j <= i; j++)
{
//print the upcounting characters
}
for (int j = 1; j <= i; j++)
{
//print the downcounting characters
}
}
}
For starters pay attention to that according to the C Standard the function main without parameters shall be declared like
int main( void )
It is not necessary that lower case letters follow each other without gaps. For example this in not correct for the EBCDIC table.
Here is a demonstrative program that shows how the required output can be obtained.
#include <stdio.h>
int main(void)
{
const char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
const size_t N = sizeof( alphabet ) - 1;
while ( 1 )
{
size_t n = 0;
printf( "Enter a non-negative number not greater than %zu (0 - exit ): ", N );
if ( scanf( "%zu", &n ) != 1 || n == 0 ) break;
n %= N + 1;
putchar( '\n' );
for ( size_t i = 0; i < n; i++ )
{
const char *p = alphabet;
do { printf( "%c ", *p++ ); } while ( p != alphabet + i + 1 );
if ( --p != alphabet )
{
do { printf( "%c ", *--p ); } while ( p != alphabet );
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
The program output might look like
Enter a non-negative number not greater than 26 (0 - exit ): 26
a
a b a
a b c b a
a b c d c b a
a b c d e d c b a
a b c d e f e d c b a
a b c d e f g f e d c b a
a b c d e f g h g f e d c b a
a b c d e f g h i h g f e d c b a
a b c d e f g h i j i h g f e d c b a
a b c d e f g h i j k j i h g f e d c b a
a b c d e f g h i j k l k j i h g f e d c b a
a b c d e f g h i j k l m l k j i h g f e d c b a
a b c d e f g h i j k l m n m l k j i h g f e d c b a
a b c d e f g h i j k l m n o n m l k j i h g f e d c b a
a b c d e f g h i j k l m n o p o n m l k j i h g f e d c b a
a b c d e f g h i j k l m n o p q p o n m l k j i h g f e d c b a
a b c d e f g h i j k l m n o p q r q p o n m l k j i h g f e d c b a
a b c d e f g h i j k l m n o p q r s r q p o n m l k j i h g f e d c b a
a b c d e f g h i j k l m n o p q r s t s r q p o n m l k j i h g f e d c b a
a b c d e f g h i j k l m n o p q r s t u t s r q p o n m l k j i h g f e d c b a
a b c d e f g h i j k l m n o p q r s t u v u t s r q p o n m l k j i h g f e d c b a
a b c d e f g h i j k l m n o p q r s t u v w v u t s r q p o n m l k j i h g f e d c b a
a b c d e f g h i j k l m n o p q r s t u v w x w v u t s r q p o n m l k j i h g f e d c b a
a b c d e f g h i j k l m n o p q r s t u v w x y x w v u t s r q p o n m l k j i h g f e d c b a
a b c d e f g h i j k l m n o p q r s t u v w x y z y x w v u t s r q p o n m l k j i h g f e d c b a
Enter a non-negative number not greater than 26 (0 - exit ): 10
a
a b a
a b c b a
a b c d c b a
a b c d e d c b a
a b c d e f e d c b a
a b c d e f g f e d c b a
a b c d e f g h g f e d c b a
a b c d e f g h i h g f e d c b a
a b c d e f g h i j i h g f e d c b a
Enter a non-negative number not greater than 26 (0 - exit ): 4
a
a b a
a b c b a
a b c d c b a
Enter a non-negative number not greater than 26 (0 - exit ): 3
a
a b a
a b c b a
Enter a non-negative number not greater than 26 (0 - exit ): 2
a
a b a
Enter a non-negative number not greater than 26 (0 - exit ): 1
a
Enter a non-negative number not greater than 26 (0 - exit ): 0
The part of the program that outputs the pattern can be placed in a separate function such a way that the pattern can be outputted in any file.
Here you are.
#include <stdio.h>
FILE * display_pattern( size_t n, FILE *fp )
{
static const char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
static const size_t N = sizeof( alphabet ) - 1;
n %= N + 1;
for ( size_t i = 0; i < n; i++ )
{
const char *p = alphabet;
do { fprintf( fp, "%c ", *p++ ); } while ( p != alphabet + i + 1 );
if ( --p != alphabet )
{
do { fprintf( fp, "%c ", *--p ); } while ( p != alphabet );
}
fputc( '\n', fp );
}
return fp;
}
int main(void)
{
while ( 1 )
{
size_t n = 0;
printf( "Enter a non-negative number (0 - exit ): " );
if ( scanf( "%zu", &n ) != 1 || n == 0 ) break;
putchar( '\n' );
display_pattern( n, stdout );
putchar( '\n' );
}
return 0;
}
Again the program output might look like
Enter a non-negative number (0 - exit ): 5
a
a b a
a b c b a
a b c d c b a
a b c d e d c b a
Enter a non-negative number (0 - exit ): 4
a
a b a
a b c b a
a b c d c b a
Enter a non-negative number (0 - exit ): 3
a
a b a
a b c b a
Enter a non-negative number (0 - exit ): 2
a
a b a
Enter a non-negative number (0 - exit ): 1
a
Enter a non-negative number (0 - exit ): 0
If actually you want to output indeed a pyramid then the function can look as it is shown in the next demonstrative program.
#include <stdio.h>
FILE * display_pattern( size_t n, FILE *fp )
{
static const char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
static const size_t N = sizeof( alphabet ) - 1;
n %= N + 1;
for ( size_t i = 0; i < n; i++ )
{
const char *p = alphabet;
fprintf( fp, "%*c ", ( int )( 2 * ( n - i) - 1 ), *p++ );
while ( p != alphabet + i + 1 ) fprintf( fp, "%c ", *p++ );
if ( --p != alphabet )
{
do { fprintf( fp, "%c ", *--p ); } while ( p != alphabet );
}
fputc( '\n', fp );
}
return fp;
}
int main(void)
{
while ( 1 )
{
size_t n = 0;
printf( "Enter a non-negative number (0 - exit ): " );
if ( scanf( "%zu", &n ) != 1 || n == 0 ) break;
putchar( '\n' );
display_pattern( n, stdout );
putchar( '\n' );
}
return 0;
}
Now the program output might look like
Enter a non-negative number (0 - exit ): 5
a
a b a
a b c b a
a b c d c b a
a b c d e d c b a
Enter a non-negative number (0 - exit ): 4
a
a b a
a b c b a
a b c d c b a
Enter a non-negative number (0 - exit ): 3
a
a b a
a b c b a
Enter a non-negative number (0 - exit ): 2
a
a b a
Enter a non-negative number (0 - exit ): 1
a
Enter a non-negative number (0 - exit ): 0
This is something for searching a substring In a 2d array
int left_to_rigth(char matrix[ROW][COLUNM], char str1[])
{
int i = 0, j, counting = 0, wordcnt;
int length = computeLength(str1); //returns legth of string
int index = -1;
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COLUNM; j += 1)
{
if (matrix[i][j] == str1[0])
{
for (wordcnt = 0; wordcnt < length; wordcnt++)
{
if (matrix[i][j + wordcnt] == str1[wordcnt])
{
counting++;
}
}
if (counting == length)
{
index = (i *12) + j;
}
}
}
}
return index;
}
The output:
Enter the string to be searched in the puzzle:
SHOUT
position in the puzzle: 12
PUZZLE(MATRIX)
X T Z M Q Y K C E C F H -->0 1 2 3 4 5 6 7 8 9 10 11
*S H O U T* E X O E A P I -->12 13 14 ------------23
X G T L Q B E L T N F K
A I R I D Z A L L I O D
M E I E T Y S E H R T I
A W B R N E T C W O H X
N O U I R U Z T S C C T
U D T P E C J I E H R U
A L E M C S Y O N I U R
L V *K E R E M* N I P H E
E A N B U R E J O N C Y
A W I I I J N J R U Y F
D W T N T H E N P J Y T
E Q L Z D I L E M M A B
R C I T E N G A M T P C
So the function returns the starting point of SHOUT which is 12 but when I try to search for the word KEREM it should give me 110 but instead it return -1 which says that the word doesnt exist.
It seems like the code only searches for the first 3 lines every input I enter after that returns -1. Could you please help I am a beginner
This is just the first part I need to make it so that it searches in every direction I can either write 4 seperate functions and call them if they dont return -1 but I need to get this working first
Okay, I've done a few speedups and simplifications.
No need for a separate counting [for left-to-right and right-to-left, at least] as you can use wordidx
Also, once you find a match on the inner loop, there is no need to continue with it. And, you can stop the outer loop early
It's faster to calculate the length of str1 outside of the call and pass length as an argument. Also, strlen should work just fine.
On left-to-right, there is no need for j to go all the way to COLUMN - 1 as the last N slots can not match if there isn't enough room on the matrix line to fulfill the remaining string length.
Also, it's undefined behavior because you'll spill over into the next row. This would be harmless [but wrong result] except for the last row, where you'll go beyond the end of the entire matrix.
So, I added a jmax value of COLUMN - length
The right-to-left is slightly trickier. The jmax trick is critical.
So, here are the two functions [they compile cleanly, but I've not tested them]:
#include <string.h>
#define ROW 10
#define COLUMN 10
int
left_to_right(char matrix[ROW][COLUMN], const char *str1, int length)
{
char *matcur;
int i;
int j;
int wordidx;
int jmax = COLUMN - length;
int index = -1;
jmax += 1;
for (i = 0; i < ROW; ++i) {
for (j = 0; j < jmax; ++j, ++matcur) {
matcur = &matrix[i][0];
if (matcur[0] != str1[0])
continue;
for (wordidx = 1; wordidx < length; ++wordidx) {
if (matcur[wordidx] != str1[wordidx])
break;
}
if (wordidx == length) {
index = (i * COLUMN) + j;
break;
}
}
if (index >= 0)
break;
}
return index;
}
int
right_to_left(char matrix[ROW][COLUMN], const char *str1, int length)
{
const char *matcur;
int i;
int j;
int wordidx;
int jmax = COLUMN - length;
int index = -1;
for (i = 0; i < ROW; ++i) {
matcur = &matrix[i][jmax];
for (j = jmax; j >= 0; --j, --matcur) {
if (matcur[0] != str1[0])
continue;
for (wordidx = 0; wordidx < length; ++wordidx) {
if (matcur[wordidx] != str1[wordidx])
break;
}
if (wordidx == length) {
index = (i * COLUMN) + j;
break;
}
}
if (index >= 0)
break;
}
return index;
}
I'm a bit confused on how to make an inverted triangle from user input so that the last character is removed each time and a space is added to the front of each row. So this is what I have now, it should be correct minus the spaces (which I can't seem to get to work for whatever reason). Should be a really simple for loop, but I just can't figure it out for the life of me.
Here's what it looks like when it's run now:
Enter a string: EXAMPLE
E X A M P L E
E X A M P L
E X A M P
E X A M
E X A
E X
E
and what I want it to look like:
Enter a string: EXAMPLE
E X A M P L E
E X A M P L
E X A M P
E X A M
E X A
E X
E
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main()
{
char string[100];
int c, k, length;
printf("Enter a string: ");
gets(string);
length = strlen(string);
printf("\n");
for(c=length; c>0; c--)
{
for(k=0; k<c; k++)
{
printf("%c ", string[k]);
}
printf("\n");
}
getch();
}
You just to add length - c spaces before printing each lines because the less characters you have to print, the more spaces you have to insert:
#include <stdio.h>
#include <string.h>
int main()
{
char string[100];
int c, k, length;
printf("Enter a string: ");
gets(string);
length = strlen(string);
printf("\n");
for(c=length; c>0; c--)
{
//Add some spaces
for(k=0; k < length - c ; k++)
{
printf(" ");
}
for(k=0; k<c; k++)
{
printf("%c ", string[k]);
}
printf("\n");
}
return 0;
}
Example with SAMPLE STRING:
Enter a string: SAMPLE STRING
S A M P L E S T R I N G
S A M P L E S T R I N
S A M P L E S T R I
S A M P L E S T R
S A M P L E S T
S A M P L E S
S A M P L E
S A M P L E
S A M P L
S A M P
S A M
S A
S
Just insert an increasing amount of spaces at every turn (i.e. printf space k times).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to get function2() to check the horizontal pairs and function3() to check the vertical pairs and pass both values back into main for function4() to use, I know not naming them properly is bad practice but bear with me...any help would be greatly appreciated...
My question is why I am getting the output of 0 for countX and countY and how to fix it..
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define maxrow 20 //defines maxrow as a constant of 20
#define maxcol 30 //defines maxcol as a constant of 30
void function1(char array[][maxcol]);
int function2(char array[][maxcol]);
int function3(char array[][maxcol]);
void function4(int, int); //function to display the pairs count
int main( void )
{
int countX = 0;
int countY = 0;
srandom( (unsigned) time(NULL) );
char array[maxrow][maxcol];
function1(array);
function2(array);
function3(array);
function4 (countX, countY);
return ( 0 ) ;
}
void function1(char array[][maxcol])
{
int x = 0;
int y = 0;
for (x=0;x<maxrow;x++)
{
for (y=0;y<maxcol;y++)
{
array[x][y] = random() % 26 + 'A';
printf("%c ", array[x][y]);
}
printf("\n");
}
printf("\n");
}
int function2(char array[][maxcol])
{
int col = 0;
int row = 0;
int countX = 0;
for (row=0;row<maxrow-1;row++)
{
for (col=0;col<maxcol; col++)
{
if (array[row][col] == array[row+1][col])
{
countX++;
}
}
}
return(countX);
}
int function3(char array[][maxcol])
{
int col = 0;
int row = 0;
int countY = 0;
for(col=0;col<maxcol-1;col++)
{
for (row=0;row<maxrow;row++)
{
if (array[row][col] == array[row][col+1])
{
countY++;
}
}
}
return(countY);
}
void function4(int countX, int countY)
{
printf("\nNumber of horizontal pairs: %d\n", countX);
printf("\nNumber of vertical pairs: %d\n\n", countY);
}
output:
N E R J B Z Y R I T Y J P P B G K R A Z X O A Y V W S E V V
M L C F U D G S U O L U Z C M C K W T N V T B V T W U L B R
I N C K U Y N A R K Q E G Q J S U T P Q G M J I K E E G P H
X Z W C K Q A Z Q T L H Y S Z H K V A B L J O W R A A X G P
H F R D H B U K A M D O V D G W K S S N U D W K C P K C O Q
U V V L B F M X R O J U E F A K B K F V Z Z B V J D M V F D
L B A I M B N A Y E R K B V P B I S N N O N O R K Z U X U Z
C F D C N P G D S G J J Q K G H N Q C B F S O T J Y S D X M
E Z T H E J Z M M R U V C L I K U V B Y W I Q M C B L W E K
L L M E U Q P V E B O Y Z Q L J D G E G E D O X P S Y A P D
L A Q X G M P W I T Z W T A P F J S L O A R R O O G H N J W
S W Y K V E W K A E F C B Y C Q D M K Q C K I T A W B H L K
F D I F N D K M N M Q U Q T T T L Y F V P H H X C I V D R J
Q X O Y E E C Q Q R D G M V C H O N F V J U E Q T G A R M S
A E R O E V S I M I A R R O M T V D I C Y R X D K S J K J X
E L B V A I R U Q F F S Y Y G K T D N B G M V F R F Z C R J
A W W D R W L K T E P A W P Y D A R I P U O D P V U W V X O
G Z M C E F B S Q U W H U S X S X Z L G O F W U X S Q T P P
H V O V X V B Y N R U J A O D X I B Y V H P D D J A X Z T M
P D H F Y H A Z H N Q C Y T S C S B D R Y M G B R R B P Q V
Number of horizontal pairs: 0
Number of vertical pairs: 0
Here is why:
function2(array); // in main
function3(array); //in main
Both these functions return something but in main you just call them and don't collect the value returned .. Change above to this is main():
countX = function2(array); // this will put the value ( countX ) returned by this function in countX
countY = function3(array); // this will put the value ( countY ) returned by this function in countY
In the functions function2 and function3 the variables countX and countY (respectively) are local variables. Just like countX and countY in main are local inside main.
One way of solving your problem is to make the variables global, but I don't recommend that. Instead I recommend that you return the variables from the function, and assign to them in main.
So e.g. in function2:
int function2(...)
{
...
return countX;
}
And in main:
countX = function2(...);
Make countX and countY globals (and don't redeclare in the functions).
Or better yet - in main():
countX = function2(array);
countY = function3(array);
And a free tip: you don't need () on all the returns...