Code giving runtime error in codeforce prob 474 A - c

I am a beginner in codeforces. I am stuck in problem 474 A where I am getting runtime error. But I can't any fault in it. I use C. Here is my code:
#include<stdio.h>
int main()
{
int a, i, j, b, arr[102];
char ch, str2[101], c;
char str1[30] = "qwertyuiopasdfghjkl;zxcvbnm,./";
for (i = 0; i<30; i++)
arr[(str1[i])] = i;
scanf("%c%s", &ch, str2);
if (ch == 'R')
{
for (i = 0; str2[i]; i++)
{
printf("%c", str1[(arr[str2[i]] - 1)]);
}
printf("\n");
}
else {
for (i = 0; str2[i]; i++)
{
printf("%c", str1[(arr[str2[i]] + 1)]);
}
printf("\n");
}
return 0;
}
Can you find what is wrong in it?

Please try this, if I understood the problem correctly this works.
// MOLE is typing to the right or to the left of 3 rows of keys
// qwertyuiop
// asdfghjkl;
// zxcvbnm,./
// encoded as: !qwertyuiop!asdfghjkl;!zxcvbnm,./!
// where '!' detects input errors
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main( void )
{
int i;
char ch;
char str2[101];
char kstr1[50];
char *poschar; // pointer to char
int int_poschar = 0;
int translated_position = 0;
int right = 0;
int left = 0;
strcpy( kstr1, "!qwertyuiop!asdfghjkl;!zxcvbnm,./!" );
printf( "\n Please enter R or L and press enter\n" );
scanf( " %c ", &ch );
ch = toupper( ch );
if ( ch == 'R' || ch == 'L' ) ; // good input say nothing
else
{
printf( "\nPlease try again with L or R ( or l or r ) " );
exit( 0 );
}
printf( "\n Please enter a word and press enter\n" );
scanf( " %100s", str2 );
strcpy( str2, strlwr( str2 ) );
if ( ch == 'R' )
{
printf( "\nRIGHT translate:\n%s\n", str2 );
right = 1;
}
else if ( ch == 'L' )
{
printf( "\nLEFT translate:\n%s\n", str2 );
left = 1;
}
getchar( );
for ( i = 0; str2[i]; i++ )
{
poschar = strchr( kstr1, str2[i] );
if ( poschar != NULL )
{
int_poschar = poschar - kstr1; // find prev pos in array
if ( right )
translated_position = int_poschar - 1;
else if ( left )
translated_position = int_poschar + 1;
else
{
printf( "\n you've got a bug !" );
exit( 0 );
}
if ( kstr1[translated_position] == '!' )
{
if ( right )
printf( "\n not possible, MOLE wrote 1 key to the right" );
else
printf( "\n not possible, MOLE wrote 1 key to the left" );
getchar( );
exit( 0 );
}
printf( "%c", kstr1[int_poschar - 1] );
} // endif ( poschar != NULL )
}
exit( 0 );
}

Related

Cannot print or access char * inside a function

The code below does not print nor return the char * temp which I am trying to retrieve from a text file containing a matrix of the form:
4
spQ77377.1 0.000000 0.776030 0.781073 0.804880
spO91086.1 0.776030 0.000000 0.564157 0.559756
spP04578.2 0.781073 0.564157 0.000000 0.302724
spO12164.1 0.804880 0.559756 0.302724 0.000000
The whole code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char * const getField( char lineBuffer[255], int field ) {
printf( "\n " );
static char temp[30];
int f = 0;
for( int i = 0; lineBuffer[i]!='\n' ; i++ ) {
if( f == field && lineBuffer[i] != ' ' ) {
temp[i] = lineBuffer[i];
printf( "%c", temp[i] );
}
if( lineBuffer[i] == ' ' && lineBuffer[i+1] == ' ' ) {
printf( "||" );
f++;
}
if( f > field ) {
printf( " break " );
printf( "*%s*", temp );
fflush(stdout);
return temp;
}
}
return "!";
}
int main() {
FILE *matrixStream;
matrixStream = fopen( "outfile", "r" );
if ( matrixStream == NULL ) {
printf( "\n\tERROR FILE NOT FOUND!\n" );
exit( 1 );
}
char lineBuffer[255];
fseek( matrixStream, 0, SEEK_SET );
int line = 0;
char ** field1 = malloc( 4 * sizeof( char *) );
while( fgets( lineBuffer, 255, matrixStream ) ) {
if( line != 0 ) {
printf( "\n line = %d ", line-1 );
if( line == 1 ) {
field1[line-1] = malloc( 30 * sizeof( char ) );
strcpy( field1[line-1], getField( lineBuffer, 1 ) );
}
}
line++;
}
printf( "\n" );
fclose( matrixStream );
return 0;
}
My objective is to have the function getField() to return a certain field or column (provided in argument as int field) for a line in the matrix.
For now, I only have the following output and can not seem to understand the problem, there are no error message during compilation with the options -Wall -pedantic. Here is the exact output:
line = 0
||0.000000|| break **
line = 1
line = 2
line = 3
The value of char * temp is not printed between the * characters as it should be.
Thank you in advance for any help or advice you can provide.
you need to terminate your temp buffer, and to have 2 indexes
static char temp[30];
int toff = 0; // temp offset
int f = 0;
for( int i = 0; lineBuffer[i]!='\n' ; i++ ) {
if( f == field && lineBuffer[i] != ' ' ) {
temp[toff++] = lineBuffer[i]; // use temp offset
printf( "%c", temp[i] );
}
if( lineBuffer[i] == ' ' && lineBuffer[i+1] == ' ' ) {
printf( "||" );
f++;
}
if( f > field ) {
temp[toff] = 0; // 0 terminate
printf( " break " );
printf( "*%s*", temp );
fflush(stdout);
return temp;
}
}
of course you should also error out if toff > sizeof(temp)
BTW the reason you get nothing is that temp, being static, is initialized to 0 at startup and you never write anything to temp[0] (beacuse you use the line buffer offset). So temp is an empty string always

Output a string with a new character in each line, forming a right triangle

Trying to output a string that has been assigned to an array, with different characters on each new line, eventually creating a right triangle. But I'm completely stuck. I believe some for loops should be involved to iterate over each character but I don't know how to increase the array index on each new line to output one character more than the line before.
This is a sketch that allowed me to visualize this:
string[0]
string[1] + string[2]
string[3] + string[4] + string[5]
string[6] + string[7] + string[8] + string[9]
For example, let's take into account this line of code: char string[50] = "Assignment";
The output desired would look like this:
A
s s
i g n
m e n t
Any guidance would be appreciated.
You can do it using only one while loop.
Here is a demonstrative program
#include <stdio.h>
void triangle_output( const char *s )
{
size_t n = 1;
size_t i = n;
while ( *s )
{
if ( i-- == 0 )
{
putchar( '\n' );
i = n++;
}
putchar( *s++ );
putchar( ' ' );
}
if ( i != n - 1 ) putchar( '\n' );
}
int main(void)
{
char *s = "Assignment";
triangle_output( s );
return 0;
}
The program output is
A
s s
i g n
m e n t
Or the function can be rewritten the following way
void triangle_output( const char *s )
{
size_t n = 1;
size_t i = 0;
while ( *s )
{
putchar( *s++ );
putchar( ' ' );
if ( ++i == n )
{
putchar( '\n' );
i = 0;
++n;
}
}
if ( i != 0 ) putchar( '\n' );
}
This function will print the triangle. If the string have enough chars to print the whole last line it will print -.
void printTriangle(const char *s)
{
size_t length = strlen(s), pos = 0;
for(size_t line = 1; line < -1; line++)
{
for(size_t ch = 1; ch <= line; ch++)
{
if(pos < length) printf("%c", *s++);
else printf("-");
pos++;
}
printf("\n");
if(pos >= length) break;
}
}
int main(void)
{
printTriangle("Assignment123");
}
https://godbolt.org/z/xxM1eEY6a

I have a question about c programming infinite loop using While

int main() {
int count[26]={0};
char input;
int i;
while(1){
scanf("%c", &input);
if(input>='a'&&input<='z') count[input-'a']++;
else if(input>='A'&&input<='Z') count[input-'A']++;
else break;
}
for (i=0; i<26; i++) {
if(count[i]!=0) {
printf("%c : %d\n", 'A'+i, count[i]);
}
}
return 0;
}
I want this code to stop when a value other than A~Z or a~z is entered.
How should I fix this code?
Here is a demonstrative program that shows how the while loop can look. I considered the space character ' ' as a valid character but it is not counted.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
unsigned int count['Z' - 'A' + 1] = { 0 };
const size_t N = sizeof( count ) / sizeof( *count );
char c;
while ( scanf( "%c", &c ) == 1 &&
( ( 'A' <= ( c = toupper(( unsigned char ) c ) ) && c <= 'Z' ) || c == ' ' ) )
{
if ( c != ' ' ) ++count[c - 'A'];
}
for ( size_t i = 0; i < N; i++ )
{
if ( count[i] )
{
printf( "'%c' : %u\n", ( char )( 'A' + i ), count[i] );
}
}
return 0;
}
If to enter this sentence
Hello World
then the program output will be
'D' : 1
'E' : 1
'H' : 1
'L' : 3
'O' : 2
'R' : 1
'W' : 1

C Program file-io user input

This program takes a a file name which contains a particular sentence and shifts by a certain amount.
if I have Hello and shift by 22 it should be Dahhk, This worked properly when I didn't enter the file name and had the file name in the program manually like
ifp = fopen( "input.txt", "r" );
However when I have the user enter the file name and the input is Hello and shift by 22 the output becomes D{‚‚…
ifp = fopen( name, "r" );
I tried using scanf("%s", name);
and I tried using fgets(name, sizeof(name), stdin);
both produce the same results
I am not sure how to fix this issue.
#include <stdio.h>
#define MAX_LEN 100
void decode( char *sentence, int shift );
int main( void )
{
FILE *ifp;
FILE *ofp;
char str[MAX_LEN];
int shift_by = 0;
char name[100];
printf("Program name: \n");
scanf("%s", name);
printf( "Please enter shift by and press enter\n" );
scanf( " %d", &shift_by );
ifp = fopen( name, "r" );
ofp = fopen( "output.txt", "w" );
if ( ifp == NULL )
{
printf( "FILE doesnt open" );
return 1;
}
while ( fgets( str, MAX_LEN, ifp ) != NULL )
{
decode( str, shift_by );
fprintf( ofp, " %s", str );
}
fclose( ifp );
fclose( ofp );
return 0;
}
void decode( char *sentence, int shift )
{
int i = 0;
char p;
while ( p = sentence[i] )
{
if ( ( p >= 'a' ) && ( p <= 'z' ) )
{
p = ( p - 'a' ) + shift % 26 + 'a';
}
if ( ( p >= 'A' ) && ( p <= 'Z' ) )
{
p = ( p - 'A' + shift ) % 26 + 'A';
}
sentence[i] = p;
i++;
}
}
OP's code has a small error:
// p = ( p - 'a' ) + shift % 26 + 'a';
p = ( p - 'a' + shift) % 26 + 'a';
Curiously OP coded correctly with
p = ( p - 'A' + shift ) % 26 + 'A';
The hint was "Hello" --> "D{‚‚…" worked for uppercase, yet not lowercase.

C source code to change first letters of words from lowercase to uppercase in a string

I have a C source code, but I have a problem with it. I want to convert the first letters of words in a string that I enter from lowercase to uppercase, but it changes all letters to uppercase. Can you help me solve this?
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
void main()
{
char sentence[100];
int count, ch, i;
int str[32];
printf("Enter a sentence \n");
for (i = 0; (sentence[i] = getchar()) != '\n'; i++)
{
;
}
sentence[i] = '\0';
/* shows the number of chars accepted in a sentence */
count = i;
printf("The given sentence is : %s", sentence);
printf("\n Case changed sentence is: ");
for (i = 0; i < count; i++)
{
ch = islower(sentence[i])? toupper(sentence[i]) : tolower(sentence[i]);
putchar(ch);
}
getch();
}
e.g.
Input: welcome to Sharif university
Desired output: Welcome To Sharif University
Actual output: WELCOME TO SHARIF UNIVERSITY
You must have check if the current char is a space and then only use toupper on the character after the space.
ch = ' ';
for (i = 0; i < count; i++)
{
ch = isspace(ch) ? toupper(sentence[i]) : tolower(sentence[i]);
putchar(ch);
}
Try the following code.:)
#include <stdio.h>
#include <ctype.h>
#define N 100
int main( void )
{
char sentence[N];
char *p = sentence;
printf( "Enter a sentence: " );
if ( !fgets( sentence, sizeof( sentence ), stdin ) ) sentence[0] = '\0';
printf( "\nThe given sentence is : %s", sentence );
do
{
while ( isblank( ( unsigned char )*p ) ) ++p;
if ( islower( ( unsigned char )*p ) ) *p = toupper( *p );
while ( *p && !isblank( ( unsigned char )*p ) ) ++p;
} while ( *p );
printf( "\nCase changed sentence is: %s", sentence );
return 0;
}
The output is
The given sentence is : welcome to Sharif university
Case changed sentence is: Welcome To Sharif University
If yor compiler does not support function isblank then you can substitute it for isspace
It seems that a more correct approach will be to use only isalpha because in general case after a blank there can be for example a digit or punctuation
#include <stdio.h>
#include <ctype.h>
#define N 100
int main( void )
{
char sentence[N];
char *p = sentence;
printf( "Enter a sentence: " );
if ( !fgets( sentence, sizeof( sentence ), stdin ) ) sentence[0] = '\0';
printf( "\nThe given sentence is : %s", sentence );
do
{
while ( *p && !isalpha( ( unsigned char )*p ) ) ++p;
if ( islower( ( unsigned char )*p ) ) *p = toupper( *p );
while ( isalpha( ( unsigned char )*p ) ) ++p;
} while ( *p );
printf( "\nCase changed sentence is: %s", sentence );
return 0;
}
If you do not want to change the original string then the code will look like
#include <stdio.h>
#include <ctype.h>
#define N 100
int main( void )
{
char sentence[N];
char *p = sentence;
printf( "Enter a sentence: " );
if ( !fgets( sentence, sizeof( sentence ), stdin ) ) sentence[0] = '\0';
printf( "\nThe given sentence is : %s", sentence );
printf( "\nCase changed sentence is: " );
do
{
while ( *p && !isalpha( ( unsigned char )*p ) ) putchar( *p++ );
if ( islower( ( unsigned char )*p ) ) putchar( toupper( *p++ ) );
while ( isalpha( ( unsigned char )*p ) ) putchar( *p++ );
} while ( *p );
return 0;
}
You need to check for characters which have a space preceding them and upper case them. You also need to check for the first character which is a special case as it does not have a space preceding it.
#include <string.h>
#include <stdio.h>
int main (void)
{
char str[] = "this is a test string";
int loop;
for (loop=-1; loop<(int) strlen(str)-1; loop++)
{
// Possible upper case required?
if (loop < 0 || str[loop]==' ')
if (str[loop+1] >= 'a' && str[loop+1] <='z')
str[loop+1] = (str[loop+1] - 'a') + 'A';
}
printf ("string is : %s\n", str);
return 0;
}
Output:
string is : This Is A Test String

Resources