Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
So this loop is supposed to count the number of numerical chars in a line, but it prints the same value each time (6356732). What am I doing wrong?
if((i >= '0') && (i <= '9'))
{
printf("%c\n", i);
count = count++;
}
count is just declared 'int count = 0;'.
edit; I made a change suggested below but the output hasn't changed?
while(fscanf(f, "%c\n", &i) !=EOF)
{
if((i >= '0') && (i <= '9'))
{
count = 0;
sum = 0;
printf("%c\n", i);
count++;
sum++;
}
}
edit 2; OK so I've got the program working as intended with the help of all you lovely folk! Thankyou very much!
while(fscanf(f, "%c\n", &i) !=EOF)
{
if((i >= '0') && (i <= '9'))
{
printf("%c\n", i);
count++;
}
}
count is defined at zero at the start of the program.
count = count++;
is undefined behaviour.
For your purpose, you just need:
count++;
Here's a simple example to read a line from stdin and count chars (which you can easily modify to suit your file reading and counting):
#include <stdio.h>
int main(void)
{
char line[256];
int count = 0;
int sum = 0;
size_t i = 0;
if (fgets(line, sizeof line, stdin)) {
while(line[i]) {
if((line[i] >= '0') && (line[i] <= '9')) {
printf("%c\n", line[i]);
count++;
sum = sum + line[i] - '0';
}
i++;
}
}
printf("%d %d\n", sum, count);
return 0;
}
You should just use count++; or count = count + 1;, because count = count++; is not defined. You can read more about the subject in this post.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
The following code is for searching a string or character in another string :-
`
#include<stdio.h>
#include<string.h>
int strindex( char primary[] , char searchfor[]){
int k;
for(int i = 0 ; primary[i] != '\0' ; i++){
for(int j = i , k = 0 ; (searchfor[k] != '\0') && (primary[j] == searchfor[k]) ; j++ , k++){
;
}
if( k > 0 && searchfor[k] == '\0'){
return i;
}
}
return -1;
}
int line( char *str ){
int i = 0;
char c;
for (;(c = getchar()) != '\n' && (c != EOF) ; i++){
str[i] = c;
}
if( c = '\n') str[i] = '\n';
str[++i] = '\0';
return i;
}
int main(){
char str1[100] , str2[100];
int i ;
puts("Enter the main string:-");
line(str1);
puts("Enter string to search for:-");
line(str2);
i = strindex(str1 , str2);
if( i >= 0){
printf("String found at index : %d \n",i);
}else{
puts("stirng not found.");
}
puts("");
return 0;
}
`
The compile result :-
PS E:\Workspace\C> gcc p1.c
PS E:\Workspace\C> ./a.exe
Enter the main string:-
this is a demo text
Enter string to search for:-
demo
PS E:\Workspace\C>
I am using visual studio code ,
As we can see that the "if-else" and "print" statements after the function call are not being executed . I have tried a lot , but could not make it . Please someone help me and tell me where I am going wrong ??
int k;
for (int j = i, k = 0; ...) { blahBlahBlah(); }
doSomethingWith(k);
That k in the for loop is not the k declared before it, it's a new variable that shadows (hides) the original. That's because using int inside the for loop is a new declaration for all variables following it.
Then, when it comes time to doSomethingWith(k), that's using the original k, as the other one has gone out of scope.
And, since that original k will have some arbitrary value, hilarity will ensue :-)
The quickest solution is probably to remove the declaration from the for statement:
int j, k;
for (j = i, k = 0; ...) { blahBlahBlah(); }
doSomethingWith(k);
In your case, that would be along the lines of:
int strindex(char *primary, char *searchfor) {
int j, k;
for (int i = 0; primary[i] != '\0'; i++) {
for (j = i, k = 0; (searchfor[k] != '\0') && (primary[j] == searchfor[k]); j++, k++);
if (k > 0 && searchfor[k] == '\0') return i;
}
return -1;
}
As an aside, this is also a problem (in line()):
if (c = '\n') str[i] = '\n'; // First = should be ==, anyway.
str[++i] = '\0';
It preserves the newline character so that you would be looking for "demo\n" in "this is a demo text\n", which won't be found. Better to just use:
str[i] = '\0';
And, on top of that, c should be an int, not a char. That's because it has to cater for all possible char values and EOF.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
#include<stdio.h>
#define MAX 10000
#define CHECK 1000
#define OPN 1
#define CLS 0
char program[MAX];
void si_qoute (void);
void do_qoute (void);
void getprogram (void);
int main(){
printf ("Type your code\n");
getprogram ();
si_quote ();
do_quote ();
return 0;
}
void si_quote (void){
int opn[CHECK], cls[CHECK], i, lcnt = 0, opn_cnt = 0;
for (i = 0; i < CHECK; i++)
opn[i] = cls[i] = 0;
for (i = 0; program[i] != '\0'; i++){
if (program [i] == '\n')
lcnt++;
if (program[i] == '\''){
opn[opn_cnt] = lcnt;
if (program[i + 1] == '\\' && program[i + 3] =='\''){
cls[opn_cnt++] == lcnt;
i += 3;
}
else if(program[i + 2] == '\''){
cls[opn_cnt++] == lcnt;
i += 2;
}
else
opn_cnt++;
}
}
opn[opn_cnt] = -1;
for(i = 0; opn[i] != -1; i++)
if (opn[i] && cls[i] == 0)
printf ("Single Quote opened at line %d not closed \n", opn[i]);
}
void do_quote (void){
int opn[CHECK], cls[CHECK], i, lcnt = 0, opn_cnt = 0;
for (i = 0; i < CHECK; i++)
opn[i] = cls[i] = 0;
for (i = 0; program[i] != '\0'; i++){
if (program[i] == '\n')
lcnt++;
if (program[i] == '\"'){
opn[opn_cnt] = lcnt;
while (program[++i] != '\"'){
if (program[i] == '\\' && program [i + 1] == '\"')
i++;
else if (program[i] == '\n'){
opn_cnt++;
lcnt++;
break;
}
}
if (program[i] == '\"')
cls[opn_cnt++] = lcnt;
}
}
opn[opn_cnt] = cls[opn_cnt] = -1;
for (i = 0; opn[i] != -1; i++)
if (opn[i] && cls[i] == 0)
printf ("Double inverted quoted opened at %d not closed \n", opn[i]);
}
void getprogram (void){
int i, c;
for (i = 0; i < MAX - 1 && (c = getchar()) != EOF; i++)
program[i] = c;
program[i++] = '\n';
program[i] = '\0';
}
I am getting and error which states implicit declaration of two functions si_quote() and do_quote(). What I am intending to do is to call these functions from main. While calling a function as per my knowledge we need to try write the function name along with the argument list within parenthesis. However as these functions doesn't accept any arguments, the parameter list is empty. I think the functions are probably getting redeclared with int as their return type. I can't understand what is really the issue with it. If it is getting redeclared as per my guess why is getprogram() still working fine. I would really appreciate if someone could help me out.
You have si_quote in one place and si_qoute in the other. These are not equal.
This simple typo makes the compiler think you declare (but not use) two of the functions and use (but do not declare) two other, completely unrelated functions.
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 try to print out the atoi value from the function that is shown in The C Programming Language but I found out that the previous format for main cant print out the atoi value.
Previously, I have write htoi in C with this format but this time it don't work. The code is shown below:
#include <stdio.h>
#include <ctype.h>
#define MAXLINE 1000
int get_line(char line[], int maxline);
int atoi(char s[]);
int main(void)
{
int len;
char line[MAXLINE];
while ((len = get_line(line, MAXLINE)) > 0) {
atoi(line);
printf("%s", line);
}
return 0;
}
int get_line(char s[], int lim)
{
int c, i;
for (i = 0; i < lim-1 && (c=getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
int atoi(char s[])
{
int i, n, sign;
for (i = 0; isspace(s[i]); i++)
;
sign = (s[i] == '-') ? -1 : 1;
if (s[i] == '+' || s[i] == '-')
i++;
for (n = 0; isdigit(s[i]); i++)
n = 10 * n + (s[i] - '0');
return sign * n;
}
It copy the user's input instead of print out the int value converted by atoi which is not my desired result.
You need to assign the result of atoi() to a variable and print that.
while ((len = get_line(line, MAXLINE)) > 0) {
int num = atoi(line);
printf("%d\n", num);
}
As you wrote in your code, atoi returns an int. At that point you have two solutions :
A . You store the returned value in a variable then you print it.
B . You print it without storing it using :
printf("%d",atoi(string));
Note : if you weren't aware of its existence, there is a already a function atoi() in stdlib.h.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to get string(including white spaces)input save to 2-dimensional array.
my code is this :
char a[10000][6];
scanf("%d", &n);
for (int i = 0;i < n;i++)
{
scanf("%[^\n]s", a[i]);
}
for (int i = 0;i < n;i++)
{
printf("%s\n", a[i]);
}
What I got that is wrong output.Please give me any suggestion!
Use
char a[10000][6];
scanf("%d",&n);
int i;
for (i = 0;i < n;i++)
{
scanf("%s",&a[i]);
}
I recently solved this question in my program. I came to the conclusion that the best way to read input from keyboard is to handle character one by one. You can use code below.:
bool in()
{
int i;
int a;
int len;
int max;
char *text[10000];
//change 'max' somehow here before loop
for(i = 0, len = 0; i < max; i++)
{
text[i] = NULL;
do
{
do
{
a = getchar();
}
while(a == '\n' && !text[i]);
if(text[i])
{
len = strlen(text[i]);
len++;
}
else
{
len = 1;
}
text[i] = realloc(text[i], sizeof(char)*(len+1));
if(!text[i])
{
printf("cant realloc\n");
return false;
}
if(a != '\n')
{
text[i][len-1] = (char) a;
text[i][len] = '\0';
}
else
{
text[i][len-1] = '\0';
}
}
while(a != '\n');
}
return true;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Pseudocode explanation of what I am attempting to do.
Convert a character array into an integer array
Iterate through each integer in that array and add q to it, unless that integer + q exceeds an upper bound. If it exceeds that number, return the modulus and add the modulus to a lower bound.
print the converted integer array in its ASCII sequence using %c.
Here is the example:
int main(void)
{
char char_message[] = "abcyzABCYZ";
int q = 10;
int i;
int message[10];
int n = strlen(char_message);
for(i = 0; i < n; i++) {
message[i] = atoi(&char_message[i]);
}
for(i = 0; i < n; i++) {
if(message[i] <= 90) {
if (message[i] + q <= 90) {
message[i] = message[i] + q;
}
else
message[i] = 65 + (message[i] % 90);
}
else if(message[i] >= 97) {
if (message[i] + q <= 122) {
message[i] = message[i] + q;
}
else
message[i] = 97 + (message[i] % 122);
}
}
for(i = 0; i < n; i++) {
printf("%c", message[i]);
}
return 0;
}
EDIT: Below is a second attempt at this problem --------------------------
int main(int argc, char *argv[]) {
if(argc != 3) {
printf("Enter an integer followed by a string \n\n");
return 1;
}
int i;
int offset = atoi(argv[1]);
char **p_message;
p_message = &argv[2];
char encrypt[strlen(*p_message)];
printf("You Entered: %d, %s \n", offset, *p_message);
for(i = 0; i < strlen(*p_message); i++)
{
encrypt[i] = ((*p_message[i] + offset) % 26);
}
for(i = 0; i < strlen(*p_message); i++)
{
printf("%c", encrypt[i]);
}
return 0;
}
I'm not doing your homework for you, but it is obvious that two things are clearly wrong.
Your use of atoi is incorrect. It should not be here at all.
Your calculation of the resulting enciphered character is wrong as well. The modulus placement is wrong in two different locations.
Your use of "magic numbers" in this code is rampant making it much, much more difficult to read than need-be. Avoid using magic numbers.
The following "enciphers" your test string via a simple forward scanning loop, output each resulting character one at a time. I've left the storage to a separate int array for you to handle. Of note the first if-block is expanded out statement by statement so you can see what is going on one step at a time. The second (lower case handling) is done in a single expression. Other than different ranges, the two methods of calculation are equivalent.
Note: this only works on platforms where character ranges A..Z and a..z are continuous. The language standard makes no enforcement of this; it only enforces it for digit characters 0..9. Thus, don't blame me if you run this on an AS/400 or OS/390 (both EBCDIC platforms) and it doesn't work.
#include <stdio.h>
#include <string.h>
int main()
{
char message[] = "abcyzABCYZ";
const int q = 10;
puts(message);
for(const char *p = message; *p; ++p)
{
int c = (unsigned char)*p;
if (c >= 'A' && c <= 'Z')
{
c -= 'A';
c += q;
c %= ('Z' - 'A' + 1);
c += 'A';
}
else if (c >= 'a' && c <= 'z')
c = ((c - ('a' - q)) % ('z' - 'a' + 1)) + 'a';
// else nothing. keep as-is
fputc(c, stdout);
}
fputc('\n', stdout);
return 0;
}
Output
abcyzABCYZ
klmijKLMIJ