Controlling user input in regards to strings in bidimensional arrays - c

I'm trying to allow the user to only write names of students in uppercase in C.
I've tried :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main() {
int number_ofstudents, i, j;
char Name[100][20];
do {
printf("Enter number of students \n");
scanf("%i", &number_ofstudents);
} while ((number_ofstudents < 0) || (number_ofstudents > 100));
for (i = 0; i < number_ofstudents; i++) {
for (j = 0; j <= strlen(Name[i]) - 1; j++) {
printf("Enter the name of student number %i in uppercase \n", i + 1);
do {
fflush(stdin);
fgets(Name[i], 20, stdin);
} while ((Name[i][j] < 'A') || (Name[i][j] > 'Z'));
}
}
return 0;
}
I've also tried:
for (i = 0; i < number_ofstudents; i++) {
for (j = 0; j <= Name[i][j] != '\0'; j++) {
printf("Enter the name of student number %i in uppercase \n", i + 1);
do {
fflush(stdin);
fgets(Name[i], 20, stdin);
} while ((Name[i][j] < 'A') || (Name[i][j] > 'Z'));
}
}
What I'm expecting is for the program to keep demanding that I enter the name of the student number x until my input is all in uppercase, however once I execute none of my inputs are accepted and the phrase "Enter the name of student number 1 in uppercase " keeps repeating itself.
Does the issue lie in my 2nd for loop when I'm trying to manipulate the letter input? I haven't learned pointers or functions yet.

Related

How to count uppercase letter in each word in a 2D string

I was trying to find out uppercase letters in each word in 2D string using C. But I'm not figuring out with the logic. I was assuming this result -
Sample Input
2
Dhaka
apple
Sample Output
1
0
Here's my code -
#include<stdio.h>
#include<string.h>
int main()
{
int count = 0, num;
char str[50][50];
printf("How many word you want to enter: ");
scanf("%d", &num);
printf("\nEnter any word: ");
for(int i=0; i<num; i++)
{
for(int j=0; j<num; j++)
{
gets(str);
}
}
for(int i=0; str[i]!='\0'; i++)
{
for(int j=i+1; j<num; j++)
{
if(str[i] >= 'A' && str[i] <= 'Z')
{
count++;
}
}
}
printf("\n%d\n", count);
return 0;
}
Source :
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main(){
int count = 0, num;
//Enter how many lines you want to enter
printf("How many lines you want to enter: ");
scanf("%d", &num);
char str[num][100];
//printf("\nEnter any word: ");
for(int i=0; i<num; i++){
//flushs the previous buffer
getchar();
//Press enter to enter a line
scanf("%[^\n]",str[i]);
}
for(int i=0;i<num; i++,count=0){
for(int j=0; str[i][j]!='\0'; j++){
//printf("%c",str[i][j]);
//if(str[i][j] >= 'A' && str[i][j] <= 'Z'){
// count++;
//}
if( isupper(str[i][j]) )
count++;
}
printf("\n%d\n",count);
}
return 0;
}
Output :
$ gcc string.c && ./a.out
How many lines you want to enter: 3
djjdjd JJ
j3j3j3j JJJJJKK
$#jjd
2
7
0

Bug Histogram Display in C

i want to do a histogram vertical display of my "election".
My code work just it seems to display wrongly the vote for the 10,11,12,13 value because i think there is 2 numbers ...
thank you for your time ;)
int main() {
char character ;
int TAB[12] = {0} ;
int vote = 0;
int I;
printf("Please enter a character:\n"); //character for the display
scanf(" %c", &character); // character we want to display in the histogram
printf("Please enter votes\n");
while(1) {
scanf("%d", &vote);
if (vote == -1) {
break;
}
TAB[vote-1]++; //save the vote into the array
}
printf("Histogram :\n");
/* Search for the maximum value */
int MAX=0;
for (I=0; I<12; I++)
{
if(TAB[I]>TAB[MAX]) MAX=I;
}
int maximum = TAB[MAX]; // maximum value
while (maximum > 0) {
for (I = 0; I < 12; I++) {
if (TAB[I] == maximum) {
printf("%c ",character);
TAB[I] = (TAB[I] - 1) ;
}
else {
printf(" ");
}
}
maximum= maximum - 1;
printf("\n");
}
for (I = 0; I < 13; I++) {
printf("%d ",I+1); // display the number of each candidat
}
printf("\n"); // go to the line
return 0;
}
You shouldn't use magic number 12, use like #define VOTE_MAX 13
For the range of vote, use if (vote <= 0 || vote > VOTE_MAX)
Format output, like printf("%2c ", character);
The line for (I = 0; I < 13; I++) { should be for (I = 0; I < 12; I++) {
The following code could work:
#include <stdio.h>
#define VOTE_MAX 13
int main() {
char character;
int TAB[VOTE_MAX] = {0};
int vote = 0;
printf("Please enter a character:\n"); // character for the display
scanf(" %c", &character); // character we want to display in the histogram
printf("Please enter votes\n");
while (1) {
scanf("%d", &vote);
if (vote <= 0 || vote > VOTE_MAX) {
break;
}
TAB[vote - 1]++; // save the vote into the array
}
printf("Histogram :\n");
/* Search for the maximum value */
int MAX = 0;
for (int i = 0; i < VOTE_MAX; ++i) {
if (TAB[i] > TAB[MAX]) MAX = i;
}
int maximum = TAB[MAX]; // maximum value
while (maximum > 0) {
for (int i = 0; i < VOTE_MAX; ++i) {
if (TAB[i] == maximum) {
printf("%2c ", character);
--TAB[i];
} else {
printf("%2c ", ' ');
}
}
--maximum;
printf("\n");
}
for (int i = 0; i < VOTE_MAX; ++i) {
printf("%2d ", i + 1); // display the number of each candidat
}
printf("\n"); // go to the line
return 0;
}
You need to add an extra space for the cases where I is 10 or higher.
Like:
for (I = 0; I < 12; I++) {
if (TAB[I] == maximum) {
printf("%c ",character);
TAB[I] = (TAB[I] - 1) ;
}
else {
printf(" ");
}
if (I >= 9) printf(" "); // Add extra space for 10, 11 and 12
}
BTW: your input method should be improved. Currently the user can enter values that will make your write outside the array. At least do something like:
while(1) {
if (scanf("%d", &vote) != 1) break; // Catch illegal input
if (vote <= 0 || vote >= 13) { // Only allow vote to be 1, 2, …, 10, 11, 12
break;
}
TAB[vote-1]++; //save the vote into the array
}

C Print Data from loop after loop ends

So, I managed to wrangle some code to get part of my program working. My program has to have a prompt to enter grades (done), repeat in a loop until broken (doneish), and print results of each grade entered. The last part is where I am stuck. I can't seem to find a good way to get any grade I entered to print at the end. I just want a "you entered ###, ###, ###," or something similar, but it can be up to 100 numbers. Below is what I have so far
#include <stdio.h>
#define MAX_ARRAY_SIZE 100
int main(void) {
int grade[MAX_ARRAY_SIZE];
int entryCount = 0;
char continueResponse;
printf("Enter an grade of between 1 and 100. \n");
printf("Enter a maximum of %d grades. \n", MAX_ARRAY_SIZE);
int i;
for(i = 0; i < MAX_ARRAY_SIZE; i++) {
printf("Enter grade: ");
scanf("%d", &grade[i]);
printf("Continue? (y/n): ");
scanf(" %c", &continueResponse);
entryCount++;
if(continueResponse == 'n' || continueResponse == 'N') {
printf(" == End of Data Entry ==\n\n");
break;
}
}
return 0;
}
Keep in mind this is third week of doing this, so I know next to nothing. If there's a "why did you do this like this", the answer is because that's how I've done it before and it works. I appreciate any input!
After your dataentry loop:
for(i = 0; i < MAX_ARRAY_SIZE; i++) {
...
}
You just have to add a second loop:
for(i = 0; i < entryCount; i++) {
printf ("%d ", grade[i]);
}
You've recorded entryCount entries to the array, numbered 0 ... entryCount - 1; you'd use another for loop to print them. For nicer formatting we do not print ", " after the last number:
printf("You've entered ");
for (i = 0; i < entryCount; i++) {
if (i == entryCount - 1) {
printf("%d", grade[i]);
}
else {
printf("%d, ", grade[i]);
}
}
printf("\n");
what I understood about your problem the following code would work:
#include <stdio.h>
#define MAX_ARRAY_SIZE 100
int main(void) {
int grade[MAX_ARRAY_SIZE];
int entryCount = 0;
char continueResponse;
printf("Enter an grade of between 1 and 100. \n");
printf("Enter a maximum of %d grades. \n", MAX_ARRAY_SIZE);
int i;
for(i = 0; i < MAX_ARRAY_SIZE; i++) {
printf("Enter grade: ");
scanf("%d", &grade[i]);
printf("Continue? (y/n): ");
scanf(" %c", &continueResponse);
entryCount++;
printf("you entred:\n");
for(int j=0;j<entryCount;j++)
{
printf("%d ",grade[j]);
}
if(continueResponse == 'n' || continueResponse == 'N') {
printf(" == End of Data Entry ==\n\n");
break;
}
}
return 0;
}

How do I print a string's characters in a diagonal pattern?

I am trying to write a program that reads a string from the user and prints the string's characters in a diagonal pattern. I set the maximum length of the string as 50 characters. The program can print the characters in a diagonal pattern, but it doesn't print the characters correctly.
#include<stdio.h>
int main () {
int i = 0, j = 0, m;
char c[50];
printf("Enter a string: ");
scanf("%c", c);
m = sizeof(c) / sizeof(c[0]);
for (i = 1; i <= m; i++) {
for (j = 1; j <= i; j++) {
if (j == i) {
printf("%c", c[i-1]);
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
Check the code below:
#include<stdio.h>
int main ()
{
int i=0,s=0;
char c[50];
printf("Enter a string: ");
scanf("%s",c);
while(c[i] != '\0')
{
s = i;
while(s--)
printf(" ");
printf("%c\n",c[i]);
i++;
}
return 0;
}

Problems with strings (do/while loop and if logic)

This code glitches on two things that I cannot diagnose, but is isolated to the if statement: if (strlen(strA) > 8). Suppose I enter in the string "1234567*aA", the program tells me the password is too long, so then I enter in the string "12345", which then triggers the else if statement: else if (strlen(strA) < 9). But everything in the while loop while (j<=9) gets triggered (the program tells me there is a number, a special character, a lowercase letter, and an uppercase letter) and the do/while loop finishes, and the program prompts me to confirm the password. That's wrong. It should prompt me to enter in the password again.
The second issue I notice is if I enter in a string "1111111111111111111", which is obviously too long, the program says the password is too long, but the entire do/while loop terminates and it asks me to confirm my passcode. It should ask me to enter a password again.
If I take out the if statements: if (strlen(strA) > 8) and else if (strlen(strA) < 9), and just run the while loop: while (j<=9), the program works fine, as long as I don't enter in too many characters in the string.
Can anyone diagnose the problem?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char strA[10];
char strB[10];
char strC[] = {'1','2','3','4','5','6','7','8','9','0'};
char strD[] = {'!','#','#','$','%','^','&','*','(',')'};
char strE[] = {'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'};
char strF[] = {'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'};
int i, j, k;
do {
k = 0;
j = 0;
printf("Please enter your password: ");
scanf("%s", &strA);
printf("%s\n", strA);
if(strlen(strA) > 8) {
printf("That password is too long\n");
}
else if(strlen(strA) < 9) {
while (j <= 9) {
for(i = 0; i <= 9; i++) {
if(strA[j] == strC[i]) {
printf("there is a number in this string.\n");
k++;
j = 0;
while (j <= 9) {
for(i = 0; i <= 9; i++) {
if(strA[j] == strD[i]) {
printf("there is a character in this string.\n");
k++;
j = 0;
while(j <= 9) {
for(i = 0; i <= 25; i++) {
if(strA[j] == strE[i]) {
printf("there is a lowercase letter in this string.\n");
k++;
j = 0;
while(j <= 9) {
for(i=0;i<=25;i++) {
if(strA[j] == strF[i]) {
printf("there is an uppercase letter in this string.\n");
k++;
}
}
j++;
}
}
}
j++;
}
}
}
j++;
}
}
}
j++;
}
if(k < 4) {
printf("Your password must contain at least one uppercase letter, one lowercase letter, a number, and a special character.\n");
}
}
} while(k < 4);
printf("Please confirm your password: ");
scanf("%s",&strB);
while(strcmp(strA, strB) != 0) {
printf("%s\n",strB);
printf("Your passwords do not match.\nPlease confirm your password: ");
scanf("%s",&strB);
}
putchar('\n');
printf("%s\n", strA);
printf("%s\n", strB);
return 0;
}
I think following line is creating the problem:
char strA[10];
char strB[10];
Initialize with default values
memset(&strA,'\0', sizeof(strA));
memset(&strB,'\0', sizeof(strB));
Look at ASCII standards : there is a faster way to test if a char c is a digit (c>47 && c<58)! http://en.wikipedia.org/wiki/ASCII
More : look at ctype.h as stated here Determine if char is a num or letter
http://www.cplusplus.com/reference/locale/isalpha/
isalpha(c) : true if letter
isdigit(c) : true if digit
``isupper(c)` : true if upper case
islower(c) : true if lower case
Bye,
Francis

Resources