Problem Statement
I'm facing difficulty in solving a programming contest problem, which reads as follows:
You are given T name(s) in english letters. Each name will include some
of the uppercase letters from A to Z, some of the lowercase letters
from a to z and some spaces. You have to transform the name(s) from
lowercase to uppercase. Letters that are originally uppercase
will remain the same and the spaces will also remain in their
places.
Sample Input-Output
If I type this in...
5
Hasnain Heickal Jami
Mir Wasi Ahmed
Tarif Ezaz
Mahmud Ridwan
Md Mahbubul Hasan
the computer should output this...
Case 1: HASNAIN HEICKAL JAMI
Case 2: MIR WASI AHMED
Case 3: TARIF EZAZ
Case 4: MAHMUD RIDWAN
Case 5: MD MAHBUBUL HASAN
Note that exactly one space is required between the semi-colon and the initial letter of the name.
My Coding
This is what I've coded in C:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
int main(void)
{
int T, i;
char string [100];
scanf("%d", &T);
for (i=0; i<T; i++)
{
gets(string);
printf("Case %d: ", i);
while (string[i])
{
putchar (toupper(string[i]));
i++;
}
printf("\n");
}
getch();
return 0;
}
Now, this code fails to produce the desired output. Where am I doing it wrong? Is there any matter with my syntax? Can somebody guide me? Please bear in mind that I'm a middle-schooler and just a beginner in C.
You need to cycle over each letter of the string one-by-one.
In this code below, I have done that with variable K, which goes from 0 to the length of the string.
Variable I keeps track of the number of strings.
int main(void)
{
int T, i, k;
char string [100];
scanf("%d", &T);
for ( i = 0; i < T; ++i)
{
gets (string);
for(k=0; k<strlen(string); ++k)
{
putchar (toupper(string[k]));
}
}
getch();
return 0;
}
In response your question: IDEOne Link
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
int T, i,k;
char string [100];
scanf("%d ", &T);
for ( i = 0; i < T; ++i)
{
gets (string);
printf("[%d] : %s\n", i, string);
for(k=0; k<strlen(string); ++k)
{
putchar (toupper(string[k]));
}
putchar('\n');
}
return 0;
}
Please go through the code and implement the test cases scenarios as per your requirement.
#include <stdio.h>
#include<string.h>
int main(){
char string[100];
int i;
scanf("%s",string);
for(i=0;i<strlen(string);i++){
string[i]=string[i]-32;
}
printf("%s",string);
return 0;
}
Related
I am writing a program like hangman in c and I am trying to run it .The problem is that it's working fine until I give it a letter to quess the word but then it crashes with -1073741819 (0xC0000005). Can someone help me solve this, I think its something really small that I cant
see . Thank you for helping me!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Rules(void);
void maskWord (char starword[], int size);
int playRound(char starword[], char answer[]);
void updateStarWord(char starword[], char answer[], char userguess);
int occurancesInWord(char userguess, char answer[]);
int c=0;
char answer[128];
int N;
int main()
{
int ch,size;
char starword[1000];
do
{
printf("---Welcome to Hangman!---\n");
printf("1.Start Game\n2.Instructions\n3.Exit\n");
scanf("%d",&ch);
if(ch>0&& ch<4)
{
switch(ch)
{
case 1:
maskWord(starword,size);break;
case 2:
Rules();break;
}
}
else
system("cls");
}
while(ch!=3);
return 0;
}
void Rules(void)
{
do
{
do
{
printf("\nThe word to guess is represented by a row of dashes representing each letter of the word.");
printf("\nRules may permit or forbid proper nouns, such as names, places, brands, or slang.");
printf("\nIf the guessing player suggests a letter which occurs in the word, the other player writes it in all its correct positions.");
printf("\nIf the suggested letter does not occur in the word, the other player draws one element of a hanged stick figure as a tally mark.\n");
}
while(getchar()!='\n');
}
while(getchar()!='\n');
}
void maskWord (char starword[], int size)
{
printf("Enter word to guess: ");
fflush(stdout);
scanf(" %s", answer);
int N = strlen(answer);
int mask[N];
for (int i=0; i < N; ++i)
{
mask[i] = 0;
}
playRound(mask,N);
}
int playRound(char starword[], char answer[])
{
// Loop over each round of guessing
int gameover = 0;
while (! gameover)
{
// Print word with *s for unguessed letters
printf("The word is : ");
for(int j=0; j < answer; ++j)
{
if (starword[j])
{
printf("%c", answer[j]);
}
else
{
printf("*");
}
}
printf("\n");
// Get player's next guess
char guess;
printf("\nGive a letter: ");
fflush(stdout);
scanf(" %c", &guess);
updateStarWord(starword,answer,guess);
}
}
void updateStarWord(char starword[], char answer[], char userguess)
{
// Mark true all mask positions corresponding to guess
int k;
for(k=0; k < answer; ++k)
{
if ((answer[k]) ==(userguess))
{
starword[k] = 1;
}
}
}
Your for loop doesn't make sense because the condition for terminating it is k < answer. You are comparing an integer (k) to a pointer (answer). The compiler should have warned you about this, so make sure your compiler warnings are turned on and you are paying attention to them. Pointers and integers are different things, and comparing them is almost never what you want to do.
If answer is null-terminated, you could probably replace that condition with answer[k]. Or maybe updateStarWord needs to take an argument that indicates the length of answer, and then the condition would be k < answer_length.
The task is: Develop a function that extracts from a sentence the first word containing a given combination of characters. Use the keyboard to enter a few sentences and letter combinations. Based on the developed function, extract from all the entered sentences all the words containing the given letter combination. Print the result of the extraction.
Here's what I already have:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int i = 0, k = 0;
char sentences[20][100], search[20];
void Input(void);
void Find(char search[]);
int main() {
system("chcp 1251");
Input();
Find(search);
return 0;
}
void Input() {
printf("Enter number of sentences:\n");
scanf_s("%d", &k);
rewind(stdin);
printf("Enter %d sentences:\n", k);
for (i = 0; i < k; i++) {
gets_s(sentences[i]);
}
rewind(stdin);
printf("Enter letter combination:\n");
gets_s(search);
}
void Find(char search[]) {
int coinc = 0;
printf("\n");
for (i = 0; i < k; i++) {
if (strstr(sentences[i], search)) {
printf("%s\n", sentences[i]);
coinc++;
}
}
printf("\nCoincidence is %found.\n", (coinc > 0) ? "":"not ");
}
Example:
Input:
Nice weather
It`s nice to see you!
Hi there
She`s a nice girl
Find:
ice
Output:
weather
It`s to see you
She`s a girl
Given task is:
Enter 10 characters. For each character entered the corresponding function prints whether it is a digit 0-9 or not.(Also I use older compiler if anyone concerns about my "gets()" and goal is to do this without pointers.)
So far I tried something like this, but for some reason it does not work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
char character[10][1];
char zero ='0';
char nine ='9';
int i;
printf("Enter 10 characters:\n");
for(i=0;i<10;i++){
gets(character[i]);
}
for(i=0;i<10;i++){
if(strcmp(character[i],zero)>=0 && strcmp(character[i],nine)<=0){
printf("%d. character '%c' is a digit.", i, character[i]);
}
else{
printf("%d. character '%c' is not a digit.", i, character[i]);
}
putchar('\n');
}
return 0;
}
Also I tried this, but it can not output correctly characters:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
char character[10][1], pom[10][1];
int num_character[10];
int i;
printf("Enter 10 characters:\n");
for(i=0;i<10;i++){
gets(character[i]);
strcpy(pom[i],character[i]);
num_character[i]=atoi(character[i]);
}
for(i=0;i<10;i++){
if(num_character[i]!=0){
printf("Character '%c' is digit.", pom[i]);
}
else{
printf("Character '%c' is not digit.", pom[i]);
}
putchar('\n');
}
return 0;
}
isdigit() also does not work after I include ctype header.
You are doing several things wrong here, gets is never recommended and fgets will put new line character in the end.
The syntax for strcmp is:
int strcmp(const char *s1, const char *s2);
You need two strings as input in strcmp but you are using a string and a character.
Here, it is better to use a character array than a 2D array.
#include <stdio.h>
int main(void)
{
char character[10]; //a simple character array
char zero ='0';
char nine ='9';
int i;
printf("Enter 10 characters:\n");
for(i=0;i<10;i++){
scanf(" %c", &character[i]); //scan characters
}
for(i=0;i<10;i++)
{
if(character[i] >= zero && character[i] <= nine) //print
printf("%d. %c is a digit \n", i+1, character[i]);
else
printf("%d. %c is not a digit \n", i+1, character[i]);
}
}
I wrote this code for a course.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i;
int numbersToPrint;
int fibonacci[50] = {0,1};
int defaultOrCustom;
printf("Do you want to run the default length of 15 numbers, or do you want to define your own length?\n1.Default\n2.Custom\n");
scanf("%i", &defaultOrCustom);
switch (defaultOrCustom){
case 1:
for (i = 2; i < 15; i++)
{
fibonacci[i] = fibonacci[i-1] + fibonacci[i-2];
printf("%i, ", fibonacci[i]);
}
break;
case 2:
printf("How many numbers in the sequence do you want to print?\n");
scanf("%i\n", &numbersToPrint);
printf("%i", numbersToPrint);
for (i = 2; i< numbersToPrint; i++)
{
fibonacci[i] = fibonacci[i-1] + fibonacci[i-2];
printf("%i\n", fibonacci[i]);
}
break;
default:{
printf("Please choose a valid option:\n");
main();
}
}
return 0;
}
My problem seems to be that the program hangs in case 2 after the line:
printf("%i", numbersToPrint);
I have verified this on both Linux and Windows, and have spoken to someone who has tried it on Mac, and he says the code works.
No errors show up however.
Any ideas how to fix it?
Change:
scanf("%i\n", &numbersToPrint);
to
scanf("%i", &numbersToPrint);
See c-faq for scanf hanging with '\n'
This is a relatively simple program for tabulating grades.
My program crashes while trying to complete the last loop, specifically at the last midterm input. Any help here?
#include <stdio.h>
#include <string.h>
#define ARRAYSIZE 2
int main(void)
{
char studentID[ARRAYSIZE][10];
int midterm[ARRAYSIZE];
int fina[ARRAYSIZE];
int i=0;
double overall[ARRAYSIZE];
for (i=0;i<ARRAYSIZE;i++)
{
printf("\nInput Student ID:");
scanf("%s",&studentID[i][10]);
printf("\nInput midterm score:");
scanf("%d",&midterm[i]);
printf("\nInput final score:");
scanf("%d",&fina[i]);
overall[i]=midterm[i]*0.3+fina[i]*0.7;
}
printf("\nStudent ID MidTerm Final Overall\n");
for (i=0;i<ARRAYSIZE;i++)
{
printf("%s%5d%5d%5f",studentID[i][10],midterm[i],fina[i],overall[i]);
}
return 0;
}
The process returned is -1073741819 (0xC0000005). Thanks.
The error is in the final printf statement, specifically 'studentID[i][10]' here you are accessing an element at an illegal index (0 to 9 are legal in your case)
please try this
//declaration
int ARRAYSIZE=2;
char studentID[ARRAYSIZE][10];
//Then, you need to enter the string into the array
int i;
for (i=0; i<ARRAYSIZE; i++) {
scanf ("%s" , studentID[i]);
}
// in oreder to print them use
for (i=0; i<ARRAYSIZE; i++) {
printf ("%s" , studentID[i]);
}