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';
Related
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 7 months ago.
Improve this question
Here is my code. It works for almost all strings...except for the ones with exclamation marks. I want to count the exclamation marks as a character but not the spaces, commas, or periods. How do I do this?
#include <stdio.h>
#include <string.h>
int main(void) {
char str[51];
fgets(str, 51, stdin);
int length = strlen(str), count = 0, i;
for (i = 0; i < length-1; i++) {
if (str[i] != ',' && str[i] != ' ' && str[i] != '.') {
count++;
}
}
printf("%d\n", count);
return 0;
}
The last character of the string is not being counted.
To go through all the string you can use i < length in the for loop
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)
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'm quite new to C and I'm trying to write a loop that takes input with getchar, then print only the U's and K's from the input using putchar.
I tried this:
printf("Enter a bunch of letters: ");
char ch;
while (ch != 'x') {
ch = getchar();
if ( ch >= 'a' && ch <= 'z') {
putchar(ch - 32);
ch;
}
}
Looks to me like you're trying to read input until 'x' is entered, then print the U's and K's from said input. Try this.
Per your comment, seems like you want to print them as upper whether or not they're read as upper. You can use tolower() for that.
char ch;
while ((ch = getchar()) != 'x')
if (toupper(ch) == 'U' || toupper(ch) == 'K')
putchar(toupper(ch));
#include <stdio.h>
int main()
{
puts("(I will print U and K only): ");
int c;
while(EOF != (c=getchar())){
if(c=='U'||c=='K')
putchar(c);
}
}
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.
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.