I'm learning basics in coding. Can any one say what went wrong with my code
Prob:Given a string, S, of length N that is indexed from 0 to N-1 , print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
char input[100], final[100];
int main()
{
int num, i, j;
char even[50], odd[50], space[] = " ";
scanf("%d", &num);
for (i = 0; i < num; i++)
{
int k = 0, p = 0;
scanf(" %[^\n]s", input);
for (j = 0; input[j] != '\0'; j++)
{
if (j % 2 == 0)
{
even[k] = input[j];
k++;
}
else
{
odd[p] = input[j];
p++;
}
}
strcat(final, even);
strcat(final, space);
strcat(final, odd);
}
printf("%s", final);
}
Related
I'm having trouble rewriting my code.
This is my program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char pole[100];
int i = 0;
char *token;
char *space = " \r\n";
while(fgets(pole, 100, stdin)){
int j = 0;
token = strtok(pole, space);
while (token != NULL){
if (strlen(token) > 0){
j++;
}
token = strtok(NULL, space);
}
if (strcmp(pole, "x") == 0 || strcmp(pole , "x\n") == 0){
break;
}
i += j;
}
for (int x = 0; x < i; x++){
printf("Word %d: \n", x);
}
return 0;
}
Input:
Hello I am human
Programming
Apple moon
x
Output:
Word 0:
Word 1:
Word 2:
Word 3:
Word 4:
Word 5:
Word 6:
But I want to write the number of strings at the beginning then the strings like this (without "x"):
3
Hello I am human
Programming
Apple moon
and get this
Word 0:
Word 1:
Word 2:
Word 3:
Word 4:
Word 5:
Word 6:
I've tried this to solve this problem, but it doesn't work
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char pole[100];
int i = 0;
int cislo;
int cislo2 = 0;
char *token;
char *space = " \r\n";
scanf("%d", &cislo);
while (cislo2 < cislo){
while(fgets(pole, 100, stdin)){
int j = 0;
token = strtok(pole, space);
while (token != NULL){
if (strlen(token) > 0){
j++;
}
token = strtok(NULL, space);
}
}
i += j;
break;
}
for (int x = 0; x < i; x++){
printf("Word %d: \n", x);
}
return 0;
}
I have no idea how to improve my program to get what I want. Please help me.
There are a few things:
You need to usewhile (cislo2 <= cislo) otherwise you'll be off by one.
Place i += j inside the inner while loop.
Why use a while loop if you break out anyway (replace the second while loop with an if)
Count up cislo2, otherwise you got yourself an infinite loop
Here is the full code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char pole[100];
int i = 0;
int cislo;
int cislo2 = 0;
char *token;
char *space = " \r\n";
scanf("%d", &cislo);
while (cislo2 <= cislo){
if (fgets(pole, 100, stdin)){
int j = 0;
token = strtok(pole, space);
while (token != NULL){
if (strlen(token) > 0){
j++;
}
token = strtok(NULL, space);
}
i += j;
}
cislo2++;
}
for (int x = 0; x < i; x++){
printf("Word %d: \n", x);
}
return 0;
}
I'm beginer in C/C++ programming.
This is my program that displays binary numbers in ascending order in the terminal (I'm compiling in Linux Mint).
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>
void reverse(char *x, int begin, int end)
{
char c;
if (begin >= end)
return;
c = *(x+begin);
*(x+begin) = *(x+end);
*(x+end) = c;
reverse(x, ++begin, --end);
}
int main()
{
unsigned int bitCount;
unsigned int naborCount;
printf("Число битов в наборе: ");
scanf("%d", &bitCount);
printf("\n");
naborCount = pow(2, bitCount);
char naborStr[bitCount*2];
for(int i = 0; i<naborCount; i++)
{
for(int j = 0; j<bitCount; j++)
{
if((i & (1<<j))==0)
{
strcat(naborStr, "0 ");
}
else
{
strcat(naborStr, "1 ");
}
if(j == bitCount-1)
{
reverse(naborStr, 0, strlen(naborStr)-1);
printf("%s \r\n", naborStr);
memset(naborStr, 0, sizeof(naborStr));
}
}
}
return 0;
}
This is what I see in the terminal
Where did this symbol come from? How to solve it?
C strings are null terminated.
The %s specifier searches for a null termination.
In your case it keeps on printing until it finds one, so you get some random symbols.
Try making use of null character at the end of the string and check.
Have a look at the following implementation:
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>
void reverse(char *x, int begin, int end)
{
char c;
if (begin >= end)
return;
c = *(x+begin);
*(x+begin) = *(x+end);
*(x+end) = c;
reverse(x, ++begin, --end);
}
int main()
{
unsigned int bitCount;
unsigned int naborCount;
printf("Число битов в наборе: ");
scanf("%d", &bitCount);
printf("\n");
naborCount = pow(2, bitCount);
char naborStr[bitCount*2 + 1]; //Increased size by 1 for null character
for(int i = 0; i<naborCount; i++)
{
for(int j = 0; j<bitCount; j++)
{
if((i & (1<<j))==0)
{
strcat(naborStr, "0 ");
}
else
{
strcat(naborStr, "1 ");
}
if(j == bitCount-1)
{
reverse(naborStr, 0, strlen(naborStr)-1);
naborStr[bitCount*2 +1] = '\0'; //Appending null character
printf("%s \r\n", naborStr);
memset(naborStr, 0, sizeof(naborStr));
}
}
}
return 0;
}
I am trying to write a function to divide a string in half but after the initial input it does not output anything. My goal is to scan a year and save the first two number and the last two numbers. This is the code:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char scan_year2() {
char year_number;
scanf("%s", year_number);
return year_number;
return 0;
}
// Function to print n equal parts of str
void divideString(char *str, int n) {
int str_size = strlen(str);
int i;
int part_size;
if (str_size % n != 0) {
printf("Invalid Input: String size");
printf(" is not divisible by n");
return;
}
part_size = str_size / 2;
for (i = 0; i < str_size; i++) {
if (i % part_size == 0)
printf("\n");
printf("%s", str[i]);
}
}
int main() {
char year_number;
scan_year2();
char str = year_number;
divideString(str, 2);
getchar();
return 0;
}
Assuming that a year is at least a 3-digit number, the best way to treat it is to treat it as a number, not as a string:
...
int year;
scanf("%d", &year);
int first = year / 100;
int last = year % 100;
printf("%d %d\n", first, last);
...
dont ignore compiler warnings, it must be complaining at you about this
char scan_year2() {
char year_number;
scanf("%s", year_number);
return year_number;
return 0;
}
you try to return twice.
Also
part_size = str_size / 2;
for (i = 0; i < str_size; i++) {
if (i % part_size == 0)
printf("\n");
printf("%s", str[i]);
}
is not going to give you the correct output. YOu are outputing the string each time. IE if str = "1923" then you will get
1923923
232
You should do
part_size = str_size / 2;
for (i = 0; i < str_size; i++) {
if (i % part_size == 0)
printf("\n");
printf("%c", str[i]);
}
to only output one char at a time
As stated in the title I am trying to find all lower-case letters that are not in a series of words. There are no upper-case letters, digits, punctuation, or special symbols.
I need help fixing my code. I am stuck and do not know where to go from here.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
int letters[26];
char words[50];
int i = 0, b = 0;
printf("Enter your input : ");
scanf("%s", words);
for(i = 0; i < 26; i++){
letters[i] = 0;
}
while(!feof(stdin)){
for(b = 0; b < strlen(words) - 1; b++){
letters[ words[b] - 'a']++;
scanf("%s", words);
}
}
printf("\nMissing letters : %c ", b + 97);
return 0;
}
My output is giving me some random letter that I do not know where it is coming from.
Here is a working first implementation.
As well as the comments that have already been made, you should use functions wherever possible to separate out the functionality of the program into logical steps. Your main function should then just call the appropriate functions in order to solve the problem. Each function should be something that is self contained and testable.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_INPUT 20 /* Max input to read from user. */
char *readinput(void);
void find_missing_lower_case(char *, int);
int main()
{
char *user_input = readinput();
int len_input = strlen(user_input);
printf("user input: %s\n", user_input);
printf("len input: %d\n", len_input);
find_missing_lower_case(user_input, len_input);
/* Free the memory allocated for 'user_input'. */
free(user_input);
return 0;
}
char *readinput()
{
char a;
char *result = (char *) malloc(MAX_INPUT);
int i;
for(i = 0; i < MAX_INPUT; ++i)
{
scanf("%c", &a);
if( a == '\n')
{
break;
}
*(result + i) = a;
}
*(result + i) = '\0';
return result;
}
void find_missing_lower_case(char *input, int len_input)
{
int a = 97; /* ASCII value of 'a' */
int z = 122; /* ASCII value of 'z' */
int lower_case_chars[26] = {0}; /* Initialise all to value of 0 */
/* Scan through input and if a lower case char is found, set the
* corresponding index of lower_case_chars to 1
*/
for(int i = 0; i < len_input; i++)
{
char c = *(input + i);
if(c >= a && c <= z)
{
lower_case_chars[c - a] = 1;
}
}
/* Iterate through lower_case_chars and print any values that were not set
* to 1 in the above for loop.
*/
printf("Missing lower case characters:\n");
for(int i = 0; i < 26; i++)
{
if(!lower_case_chars[i])
{
printf("%c ", i + a);
}
}
printf("\n");
}
I figured it out and this is the code I used.
int main(void)
{
int array[26];
char w;
int i=0;
for(i=0; i<26; i++) {
array[i]=0; }
printf("Enter your input: ");
scanf("%c", &w);
while(!feof(stdin)) {
array[w-97] = 1;
scanf("%c", &w); }
printf("Missing letters: ");
for(i=0; i<26; i++) {
if(array[i] == 0) {
printf("%c ", i+97); }
}
printf("\n");
return 0;
}
I'm trying to get the following character pattern as follows by only using "for" or "while" loops but no "if" or "if-else" statements: (Mentioned in code block)
It's difficult for me to figure out a way to make the first character appear as blank space or represent it even as a null character. Below is my code:
#include <stdio.h>
#include <conio.h>
int main()
{
char ch[6], j='\0';
int p,q,n,i;
printf("Enter a character\n");
for (n = 0; n < 5; n++)
{
scanf_s(" %c", &ch[n], 1);
}
printf("\n");
for (i = 4; i >= 0; i--)
{
for (p = 4; p >= i; p--)
{
printf("%c", ch[p+1]-1);// _-->A blank space
} // BA
printf("\n"); // CBA
} // DCBA
// EDCBA
_getch();
return 0;
}
I cannot figure out where I'm going wrong — can you help?
I changed this
printf("\n");
for (i = 4; i >= 0; i--)
{
for (p = 4; p >= i; p--)
{
printf("%c", ch[p+1]-1);
}
printf("\n");
}
for this
printf("\n_\n");
for (i = 1; i < 5; i++) {
for (p = i; p >= 0; p--) {
printf("%c", ch[p]);
}
printf("\n");
}
And got this output (console):
Enter a character
ABCDE
_
BA
CBA
DCBA
EDCBA
#include <stdio.h>
#include <conio.h>
#define SIZE 5
int main(void){
char ch[SIZE+1] = {0};
int i;
printf("Enter a character\n");
for (i = SIZE-1; i >= 0; --i){
scanf_s(" %c", &ch[i], 1);
}
printf(" \n");
for (i = SIZE-2; i >= 0; --i) {
printf("%s\n", ch + i);
}
_getch();
return 0;
}