For loop to end of array [closed] - c

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 8 years ago.
Improve this question
I'm making function. To that function i send the array and then i need to write down that array with one space between characters in it .
How can i know is it end of array or not ?
while (characters[i] != '0' )
{
printf(" %c");
i++;
}
Edit
int i=0;
while (characters[i] != 0 )
{
printf(" %c", characters[i]);
i++;
}

int arraySize = sizeof(characters)/sizeof(characters[0]);
for(int i = 0; i < arraySize; i++) {
printf(" %c", characters[i]);
}

while (characters[i] != '0' )
looks for the character 0 (which is x30). YOu need
while (characters[i] != 0 )
or
while (characters[i] != '\0' )
which looks for the character coded as 0x00.

Related

Removing `else` condition suddenly makes the code work, what gives? [closed]

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 1 year ago.
Improve this question
This code doesn't do as intended - it should print out all of plaintext. I'm guessing there's something at work under the hood, but that logic escapes me. If you remove the else condition with underlying statement, it suddenly works.
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
int main() {
string plaintext = get_string("plaintext: ");
int i;
for(i = 0; plaintext[i] != '\0'; i++)
if(isalpha(plaintext[i] != 0))
printf("%c", plaintext[i]);
//I intend to do stuff with alphabetic characters, but that code isn't relevant, so it's not included
else
printf("%c", 'a');
}
I'll be honest, this looks like magic to me. Why would adding an else condition affect whether the condition for the original if statement was met or not (if that's the case)?
Is it somehow because of using string in cs50.h?
for(i = 0; plaintext[i] != '\0'; i++)
if(isalpha(plaintext[i] != 0))
printf("%c", plaintext[i]);
else
printf("%c", plaintext[i]);
is the same as :
for(i = 0; plaintext[i] != '\0'; i++)
printf("%c", plaintext[i]);
because any logical operation may have two results: true and false. If in both cases you do exactly the same, why do you check the condition?
another problem :
if(isalpha(plaintext[i] != 0))
it should be
if(isalpha(plaintext[i]) != 0)

Printing a question mark after a loop [closed]

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 6 years ago.
Improve this question
Trying to figure out how to print the question mark as so: user inputs: "apple" and then the string gets stored in an array and gets printed like this : "apple?" here is my code:
#include<stdio.h>
int main()
{
char a[23];
int i=0;
printf("enter a single english word: ");
while( (a[i++]=getchar()) != '\n' && i < 23){
a[i] = '\0';
i = 0;
}
while(a[i] != '\0'){
printf("%c",a[i++]);
}
printf("?");
return 0;
}
You are resetting i after each character read. That is effectively making you erase whatever was stored. you need to move i = 0; from inside the first while loop to just after it.
The code you have inside the first while loop should be done after the loop is done. You do all the work of the loop in the while() header. The loop body was resetting i back to 0 each time, so you were repeatedly overwriting the first character of a.
while( (a[i++]=getchar()) != '\n' && i < 23){
}
a[i] = '\0';
i = 0;
First of all, your input loop is incorrect. It should be like so:
while( (a[i++]=getchar()) != '\n' && i < 23) {}
a[i] = '\0';
As the comments to your question say above, you are resetting the pointer index after each character is read, therefore reading character 0 of the array every time. You should do this after the loop.
Also, because a is just a string, you should be able to just print it like this (without the loop):
printf("%s?", a);
Final Program:
#include<stdio.h>
int main()
{
char a[23];
int i=0;
printf("enter a single english word: ");
while( (a[i++]=getchar()) != '\n' && i < 23) {}
a[i] = '\0';
printf("%s?",a);
return 0;
}
The follwoing logic works:
char a[23];
int i=0,j=0;
printf("enter a single english word: ");
To scan the input into an array :
for(a[i]=getchar(); (a[i]!='\n') && (i<23) ; i++)
;
And finally printing :
for(j=0;j<i;j++)
putchar(a[j]);
Hence the program :
#include<stdio.h>
int main()
{
char a[23];
int i=0,j=0;
printf("enter a single english word: ");
for(a[i]=getchar(); (a[i]!='\n') && (i<23) ; i++)
;
for(j=0;j<i;j++)
putchar(a[j]);
printf("?");
return 0;
}

Terminating a program using carriage return in C [closed]

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 7 years ago.
Improve this question
Quesion: Write a C program that will accept a line of text. Store it in an array & then write it out backwards. Allow the length of line to be unspecified terminated by carriage return but assume it will not exceed 80 characters
My Solution:
#include <stdio.h>
int main()
{
printf("\nEnter a sentence:\n");
char sent[80]; // creates an array of length 80
int i = 0;
while((sent[i] = getchar()) != '\n')
{
i++;
}
sent[i] = '\0';
printf("The Reversed sentence is : ");
for(i=i-1; i>=0; i--)
{
printf("%c", sent[i]);
}
getchar();
scanf("%c", &sent[i]);
return 0;
}
Is my code correct?
(I was wondering about carriage return part)
What if you enter exactly 80 characters??? You will end up putting '\0' at sent[81], which is very bad...
sent[80] will be the '\n' and sent[81] the '\0'
Also I am not sure why you do
getchar();
scanf("%c",&sent[i]);
at the end of your function. i at this point is -1.

rubbish instead of string display [closed]

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 9 years ago.
Improve this question
The task is to be sure that phrases are can be readed vise versa or not (without spaces ' ').
So I got the point and made some code. But when I made
printf("%s", str_1)
I found a lot of same russian letters.
For example my inputs are: '123' '321', but displayed rubbish
My str_1[i] is going to be like 123321 and then checked first and last and so on members.
After that I added in if condition if(str[i] != 'М') and now everything nice.
Please tell me what I did wrong and what is going on with "M".
I hope my question is clear.
#include "stdio.h"
#include "locale.h"
int main(){
char str[100];
char str_1[100];
int i, j, k, l;
j = 0;
l = 0;
fgets(str, 100, stdin); //we got array
//how many chars before '\0'
for(i = 0; i < 100; i++)
{
if(str[i] != ' ' && str[i] != '\n' && str[i] != 'М')
{
str_1[j] = str[i]; //made new array without spaces between words or whatever
j++;
}
}
j--;
for(k = 0; k < j; k++)
{
if(str_1[k] == str_1[j - 1 - k])
{
l++;
}
}
if( l == k)
{
printf(";) YES! good job!\n");
}
else
{
printf(";) NOT! I'm sorry!\n");
}
return 0;
}
As it seems, you skip over the end of the string, as you never check where it ends.
At least, this is my suspicion; I am not sure if it is the right reason.
You should check for str[i] == '\0' as well and then break the whole loop.
As I see, you even made this consideration in
//how many chars before '\0'
but didn't include it in code.
A
for(i = 0; i < 100 && str[i] != '\0'; i++)
should do the trick if you omit the j-- afterwards.

Replace a digit in a string with a symbol [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The following code reads and finds the digit 1. My question is how could it be replaced with a symbol (say 'a') and printed back with the replaced ones.
int i, newtxt;
char text[100];
printf("Enter text: ");
gets(text);
for(i = 0; i<strlen(text); i++)
{
if(text[i] == '1')
replace with a?
}
printf("%s", newtxt);
getch();
return 0;
}
Try this
if(text[i] == '1')
text[i] = 'a';
And do not use gets. Its unsafe. Use fgets instead.
fgets(text, 100, stdin);
Also
printf("%s", newtxt);
is wrong. You have to fix this (try it your self).
Just replace it like this:
if(text[i] == '1')
text[i] = 'a';

Resources