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 3 years ago.
Improve this question
I have an array formed from a text file imported by stdin.
The text file looks like this:
"Name"
"Number"
"Name"
"Number"
...
The entire code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char** argv)
{
//number of arguments
if (argc > 2)
{
fprintf(stderr, "Too many arguments\n");
return 1;
}
//check argument 1
{
if (argc == 2)
{
unsigned i = 0;
while (i < strlen(argv[1]))
{
if ((isdigit(argv[1][i])) == 0)
{
fprintf(stderr, "Enter a number\n");
return 1;
}
i++;
}
}
else
{
fprintf(stderr, "argument\n");
return -1;
}
}
//find \n and separate
int g = 0;
int c = 0;
char buffer[102];
char people[42][102];
char numbers[42][102];
while (fgets(buffer, sizeof buffer, stdin) != NULL)
{
if (g % 2 == 0)
{
strcpy(people[c], buffer);
//printf("%s", people[c]);
}
if (g % 2 == 1)
{
strcpy(numbers[c], buffer);
c++;
}
g++;
}
//convert and remove \n
char conv_people[42][102];
for (int i = 0; i < c; i++)
{
for (unsigned j = 0; j < strlen(people[i]); j++)
{
if (islower(people[i][j]) == 0 && people[i][j] != ' ' && people[i][j] != '.')
{
if (people[i][j] == '\n')
{
conv_people[i][j] = '\0';
}
people[i][j] = conv_people[i][j] + 32;
}
}
}
//covert to numbers
char conv[42][102];
for (int i = 0; i < c; i++)
{
for (unsigned j = 0; j < strlen(people[i]); j++)
{
if (conv_people[i][j] == ' ' || conv_people[i][i] == '.' || conv_people[i][i] == '\n' || conv_people[i][i] == '\0')
{
conv[i][j] = '0';
}
if (conv_people[i][j] == 'a' || conv_people[i][j] == 'b' || conv_people[i][j] == 'c')
{
conv[i][j] = '2';
}
if (conv_people[i][j] == 'd' || conv_people[i][j] == 'e' || conv_people[i][j] == 'f')
{
conv[i][j] = '3';
}
if (conv_people[i][j] == 'g' || conv_people[i][j] == 'h' || conv_people[i][j] == 'i')
{
conv[i][j] = '4';
}
if (conv_people[i][j] == 'j' || conv_people[i][j] == 'k' || conv_people[i][j] == 'l')
{
conv[i][j] = '5';
}
if (conv_people[i][j] == 'm' || conv_people[i][j] == 'n' || conv_people[i][j] == 'o')
{
conv[i][j] = '6';
}
if (conv_people[i][j] == 'p' || conv_people[i][j] == 'q' || conv_people[i][j] == 'r' || conv_people[i][j] == 's')
{
conv[i][j] = '7';
}
if (conv_people[i][j] == 't' || conv_people[i][j] == 'u' || conv_people[i][j] == 'v')
{
conv[i][j] = '8';
}
if (conv_people[i][j] == 'w' || conv_people[i][j] == 'x' || conv_people[i][j] == 'y' || conv_people[i][j] == 'z')
{
conv[i][j] = '9';
}
}
}
//compare
int i = 0;
while (i < c)
{
if (strstr(conv[i], argv[1]) != NULL)
printf("%s, %s", people[i], numbers[i]);
if (strstr(numbers[i], argv[1]) != NULL)
printf("%s, %s", people[i], numbers[i]);
i++;
}
return 0;
}
The program takes a list of people and their phone numbers and searches it using argv[1]
The output always omits the first capital letter in each word
So if the file contains a name like: Barrack Obama
the program returns arrack bama
The numbers and converted names are working fine
I didn't want to post the whole thing because it's extremely ugly.
I've run the code and John is output as Éohn. It likely comes from
people[i][j] = conv_people[i][j] + 32;
because you never set any values in conv_people[i] except a terminator.
If I add this first line in the loop
strcpy(conv_people[i], people[i]);
then is outputs
john
with a lower case initial letter.
Aside: it is safer and convenient to use
people[i][j] = tolower(conv_people[i][j]);
which doesn't even need to be tested to see if an uppercase letter was passed.
Related
I have write code and it producing correct output but it can not fill all requirements please optimize my code or give me some suggestion.
It is my code...
// it is my code it produces correct output but not complete requirement.
public String flipCoins(int N,String s) {
char ch[] = new char[N];
for(int i=0;i<N;i++){
ch[i] = s.charAt(i);
}
for(int i=1;i<N;i++){
if(ch[i] == '1' || ch[i-1] == '1'){
ch[i] = '1';
ch[i-1] = '1';
}else if(ch[i] == '0' || ch[i-1] == '0'){
ch[i] = '1';
ch[i-1] = '1';
}else if(ch[i] == '0' || ch[i-1] == '1'){
ch[i] = '1';
ch[i-1] = '0';
}else if(ch[i] == '1' || ch[i-1] == '0'){
ch[i] = '0';
ch[i-1]='1';
}
}
int count = 0;
for(char c: ch){
if(c == '1')
count++;
}
if(count == N)
return "Yes";
else
return "No";
}
What I learned from the question is that you can flip any number of time, so
the question boils down to find whether there are even nos. of zeroes or not beacuse you can swap 00 pair to make 11 else it would always be 10 or 01
i'm a beginner to programming and I'm running into a problem. I think that this is quite basic but I have looked around everywhere and can't find a solution (probably because of my own lack of understanding). This is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char inputString[999];
char inputArray[99][99];
int a = 0;
int b = 0;
fgets(inputString, sizeof inputString, stdin);
for(int i = 0; i <= (strlen(inputString)); i++) {
if(inputString[i] == ' ' || inputString[i] == '\0') {
inputArray[a][b] = '\0';
a++;
b = 0;
}
else {
inputArray[a][b] = inputString[i];
b++;
}
}
for (int i = 0; i < 99; i++) {
if (inputArray[i][0] == '\0') {
break;
}
for (int j = 0; j < 99; j++) {
iif (inputArray[i][0] == 'a' || inputArray[i][0] == 'e' || inputArray[i][0] == 'i' || inputArray[i][0] == 'o' || inputArray[i][0] == 'u' ||
inputArray[i][0] == 'A' || inputArray[i][0] == 'E' || inputArray[i][0] == 'I' || inputArray[i][0] == 'O' || inputArray[i][0] == 'U') {
if (inputArray[i][j] == '.') {
inputArray[i][j] = '\0';
if (inputArray[i][j] == '\0') {
inputArray[i][j] = 'm';
inputArray[i][j + 1] = 'o';
inputArray[i][j + 2] = 'o';
inputArray[i][j + 3] = '.';
inputArray[i][j + 4] = '\0';
i++;
j = 0;
}
}
else if (inputArray[i][j] == ',') {
inputArray[i][j] = '\0';
if (inputArray[i][j] == '\0') {
inputArray[i][j] = 'm';
inputArray[i][j + 1] = 'o';
inputArray[i][j + 2] = 'o';
inputArray[i][j + 3] = ',';
inputArray[i][j + 4] = '\0';
i++;
j = 0;
}
}
else {
if (inputArray[i][j] == '\0') {
inputArray[i][j] = 'm';
inputArray[i][j + 1] = 'o';
inputArray[i][j + 2] = 'o';
inputArray[i][j + 3] = '\0';
i++;
j = 0;
}
}
}
else if (inputArray[i][0] != 'a' || inputArray[i][0] != 'e' || inputArray[i][0] != 'i' || inputArray[i][0] != 'o' || inputArray[i][0] != 'u' ||
inputArray[i][0] != 'A' || inputArray[i][0] != 'E' || inputArray[i][0] != 'I' || inputArray[i][0] != 'O' || inputArray[i][0] != 'U' || inputArray[i][0] != '\0') {
printf("a");
i++;
j = 0;
}
}
}
for (int i = 0; i < 99; i++) {
printf("%s ", inputArray[i]);
if (inputArray[i][0] == '\0') {
break;
}
}
return 0;
}
Essentially I'm trying to input a string, separate them into words using a 2-D array, and then check if the first letters of each string in that array is a consonant or a vowel.
I'm using printf("a"); to test if my code works, but when i run the program and input something like "yo", this is the output:
yo
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaayo
The letter "a" is printed 99 times, which is as many times as the inner for loop is ran, so i'm guessing it has something to do with the fact that the first element of every string in the array is also not a vowel and maybe it's a null character, so I tried adding if (inputArray[i][0] == '\0') { (like in the code), but it still doesn't work.
I'd really appreciate if you guys can help me out
EDIT: I changed the code a bit to make it more readable (thanks Benjamin Maurer!), but essentially what I'm trying to do in the first if statement is add "moo" to the end of a word if it starts with a vowel.
Your code starts normal, but why did you then switch from array notation to this absolute madness in the second loop with pointer dereferences? I'm not even going to try to understand that code, bc. it's unreadable.
According to your description, each of inputArray[i] is a string. So checking if inputArray[i][0] is a consontant/vowel should suffice, no?
#include <ctype.h>
#include <stdio.h>
// ...Read input...
for (int i = 0; i < 99; ++i) {
if (isalpha(inputArray[i][0])) {
char c = toupper(inputArray[i][0]);
switch (c) {
// Intentional fallthroughs
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
puts("is vowel!");
break;
default:
puts("is consonant");
}
}
}
Both isalpha and toupper are from ctype.h.
islapha checks whether the argument is an alphabetic latin character (a letter).
This question already has answers here:
How to check if variable equal to multiple values
(5 answers)
Closed 3 years ago.
I am reading a string (array of characters in C) from the user, and I am trying to analyze the number of special characters in that string. Special characters are denoted by any character in the ASCII range that is not a vowel or a blank space.
When I use this function, I get different results even when there is no string input from the user.
int restCounter (char string[]) {
int unsigned specialchar = 0;
for (int i = 0; string[i] != '\0'; ++i) {
if(string[i] != ' ' || 'a' || 'e' || 'i' || 'o' || 'u' ||'A' || 'E' || 'I' || 'O' || 'U' || '\0') {
specialchar++;
}
else if(string[i] != 'a') {
specialchar++;
}
else if(string[i] != 'e') {
specialchar++;
}
else if(string[i] != 'i') {
specialchar++;
}
else if(string[i] != 'o') {
specialchar++;
}
else if(string[i] != 'u') {
specialchar++;
}
else if(string[i] != 'A') {
specialchar++;
}
else if(string[i] != 'E') {
specialchar++;
}
else if(string[i] != 'I') {
specialchar++;
}
else if(string[i] != 'O') {
specialchar++;
}
else if(string[i] != 'U') {
specialchar++;
}
else if(string[i] != '\0') {
specialchar++;
}
}
return specialchar;
}
C got a function to do precisely what you want - strchr :
#include <string.h>
unsigned int restCounter (char string[]){
unsigned int specialchar = 0;
for (size_t i = 0; string[i] != '\0'; ++i) {
if( 0 == strchr("aeiouAEIOU ", string[i])) {
++specialchar;
}
return specialchar;
}
Don't roll your own code if there is a standard library function accomplishing what you want.
However, if you don't just want to get the job done but learn by implementing your counting function manually, you got to understand how if works (in most programming languages).
if (s != 1) {
++special;
} else if (s != 2) {
++special;
}
Does not increase special only, if s is not 1 and not 2 at the same time. Just consider what happens if s is 2: the first if condition is true, this the first if body is executed and the else skipped.
Your else statements are superfluous, because they are flawed by this misunderstanding
if you want to increase special only if s is both not 1 and not 2, you need to program it just as you describe it in English:
if( (s != 1) && (s != 2)) {
++special;
}
Thus, if you really want to implement your counting function manually, do it like:
unsigned int restCounter (char string[]){
unsigned int specialchar = 0;
for (size_t i = 0; string[i] != '\0'; ++i) {
char c = string[i];
if (('a' != c) && ('e' != c) && ('A' != c) && ('E' != c)) {
++specialchar;
}
return specialchar;
}
It currently only uses vowels a and e, but I guess you see the pattern how to add i,o,u as well?
be aware, however, that I don't advise this solution: it's is ugly, clumsy, and it's hard to figure out what it does.
Go with the first solution unless you really want to code it manually...
Btw: you won't see a '\0' in the loop body, thus checking the case c == '\0' is unnecessary.
C Programming Language. The problem is focused on functions and recursion. The problem is just to check if the line contains 'good', regardless of capitalization. It compiles and matches the sample output but the checking software marks it incorrect.
#include<stdio.h>
#include<stdlib.h>
int checkString(char string[])
{
for(int i = 0; i<80; i++)
{
if(string[i] == 103 || string[i] == 71 && string[i+1] == 111 || string[i+1] == 79 && string[i+2] == 111 || string[i+1] == 79 && string[i+3] == 100 || string[i+1] == 68)
{
return 1;
break;
}
}
}
int goodCheck()
{
char string[80] = {'0'};
fgets(string, 80, stdin);
if(checkString(string)==1)
{
return 3;
}
else
{
return 0;
}
}
int main()
{
int cases = 0;
char string[80];
scanf("%d", &cases);
fflush(stdin);
for(int i = 1; i<=cases; i++)
{
if (goodCheck() == 3)
{
printf("Case #%d: yes\n", i);
}
else
{
printf("Case #%d: no\n", i);
}
}
}
EDIT:
This is the code post revisions, though it is still marked wrong it is graded better than before.
#include<stdio.h>
#include<stdlib.h>
int checkString(char string[]);
void goodCheck(int i);
int main()
{
int cases = 0;
char string[80];
scanf("%d\n", &cases);
for(int i = 1; i<=cases; i++)
{
goodCheck(i);
}
}
int checkString(char string[])
{
for(int i = 0; i<80; i++)
{
if((string[i] == 'g' || string[i] == 'G') &&
(string[i+1] == 'o' || string[i+1] == 'O') &&
(string[i+2] == 'o' || string[i+2] == 'O') &&
(string[i+3] == 'd' || string[i+3] == 'D'))
{
return 1;
}
}
}
void goodCheck(int i)
{
char string[80] = {'0'};
fgets(string, 80, stdin);
if(checkString(string)==1)
{
printf("Case #%d: yes\n", i);
}
else
{
printf("Case #%d: no\n", i);
}
}
&& has higher operator precedence than ||. So your code is checking
string[i] == 'g'
|| (string [i] == 'G' && string [i+1] == 'o')
|| (string [i+1] == 'O' && string [i+2] == 'o')
|| (string [i+2] == 'O' && string [i+3] == 'd')
|| string [i+3] == 'D'
All substrings starting with "g", or starting with "Go", or containing "Oo" or "Od", or ending with "D", will pass your check. Like "Goofy" will pass, or "Oh my god!".
All variations of "good" do pass, but many incorrect strings pass as well. It seems you haven't tested this with any strings that contain a few, but not all characters of "good". Even "Lady Gaga passes the test" passes the test (because of a single lowercase g).
String searching problems can be solved by finite automata:
int checkString(char *string)
{
int pos,state;
for(pos=state=0; string[pos]; pos++) {
switch(string[pos]) {
case 'G':
case 'g': if (state++ == 0) continue;
break;
case 'O':
case 'o': if (state++ == 1 || state == 3) continue;
break;
case 'D':
case 'd': if (state == 3) return pos-state; // Got it!
default:break;
}
state=0;
}
return -1; // Failed
}
Objective: create a simple code that converts in a .txt file:
every 'I', 'E', 'A', 'S' and 'O' into '1', '3', '4', '5' and '0' respectively.
At first I made it very simply, but it couldn't handle multiple lines of text. So I tried to treat the phrases and lines as a matrix, but I still can't make it work. Here is the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *arquivo;
char frase[100][100];
int i = 0;
int j = 0;
//adress of the initial text file
arquivo = fopen("C:\\Users\\xand\\Desktop\\ex1.txt", "r");
//transfering every line of the file into a matrix
while (!feof(arquivo))
{
fgets(frase[100][i],100, arquivo);
i++;
}
fclose(arquivo);
//converting the letters to numbers
for(j = 0; j < 100; j++)
{
while(frase[i][j] != '\0')
{
if(frase[i][j] == 'i' || frase[i][j] == 'I')
{
frase[i][j] = '1';
}
if(frase[i][j] == 'e' || frase[i][j] == 'E')
{
frase[i][j] = '3';
}
if(frase[i][j] == 'a' || frase[i][j] == 'A')
{
frase[i][j] = '4';
}
if(frase[i][j] == 's' || frase[i][j] == 'S')
{
frase[i][j] = '5';
}
if(frase[i][j] == 'o' || frase[i][j] == 'O')
{
frase[i][j] = '0';
}
i++;
}
}
arquivo = fopen("ex1 criptografado.txt", "w");
//here is where I believe to be the problem
//It doesn't even create the new file. Im not sure if using matrix is the ideal solution to fprintf a multi-lined text to a file
for(j = 0; j < 100; j++)
{
i = 0;
while(frase[i][j] != '\0')
{
fprintf(arquivo, "%s", frase[i][j]);
i++;
}
fprintf(arquivo, "\n");
}
fclose(arquivo);
return 0;
}
The code compiles, but crashes as I try to run it. Can anyone help me with a solution for this?
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *f, *g;
int c;
if ((f = fopen("asdf.txt", "r")) == NULL) {
perror("fopen");
exit(1);
}
if ((g = fopen("asdf1.txt", "w")) == NULL) {
perror("fopen");
exit(1);
}
while ((c = fgetc(f)) != EOF) {
switch (c) {
case 'i':
case 'I':
fputc('1', g);
break;
case 'e':
case 'E':
fputc('3', g);
break;
case 'a':
case 'A':
fputc('4', g);
break;
case 's':
case 'S':
fputc('5', g);
break;
case 'o':
case 'O':
fputc('0', g);
break;
default:
fputc(c, g);
break;
}
}
fclose(f);
fclose(g);
return 0;
}
I've fixed your code. Mainly it was adding '\0' to all unused lines, not printing '\n' at the end of lines because it's put in the string by fgets, replacing frase[i][j] with frase[j][i], and the actual crash: fprintf(arquivo, "%c", frase[j][i]) instead of fprintf(arquivo, "%s", frase[i][j]).
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *arquivo;
char frase[100][100];
int i = 0;
int j = 0;
//adress of the initial text file
arquivo = fopen("C:\\Users\\xand\\Desktop\\ex1.txt", "r");
//transfering every line of the file into a matrix
while (!feof(arquivo)) {
fgets(frase[i], 100, arquivo);
i++;
}
for (; i < 100; i++) {
frase[i][0] = '\0';
}
fclose(arquivo);
//converting the letters to numbers
for (j = 0; j < 100; j++) {
i = 0;
while (frase[j][i] != '\0') {
if (frase[j][i] == 'i' || frase[j][i] == 'I') {
frase[j][i] = '1';
}
if (frase[j][i] == 'e' || frase[j][i] == 'E') {
frase[j][i] = '3';
}
if (frase[j][i] == 'a' || frase[j][i] == 'A') {
frase[j][i] = '4';
}
if (frase[j][i] == 's' || frase[j][i] == 'S') {
frase[j][i] = '5';
}
if (frase[j][i] == 'o' || frase[j][i] == 'O') {
frase[j][i] = '0';
}
i++;
}
}
arquivo = fopen("ex1 criptografado.txt", "w");
for (j = 0; j < 100; j++) {
i = 0;
while (frase[j][i] != '\0') {
fprintf(arquivo, "%c", frase[j][i]);
i++;
}
}
// or simpler:
// for (j = 0; j < 100; j++)
// fprintf(arquivo, "%s", frase[j]);
fclose(arquivo);
return 0;
}
I think there's issue with Logic for using matrix.
You can take a chunk of string and then compare that for all characters - Change the character with Number if it's one of wanted characters (That you need to change), by writing character to stream - See fputc reference for details.
EDIT:
See the answer by #ctn - his code is good example.