Program prints the backward version of the given set of words - c

First I should enter the number of words I am going to print backwards, then enter those words and program should print them line by line backwards, for example:
3
Something
written
backwards
program prints:
gnihtemoS
nettirw
sdrawkcab
This is a piece of code I wrote, I don't really know why it doesn't work. I'm totally newbie in programming, thanks for help.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
int i, j, n;
char str[100]="\0";
char buff[100];
scanf("%d", &n);
printf("Enter String\n");
for(int i=0;i<=n; i++)
{
fgets(buff, sizeof buff, stdin);
strcat(str, buff);
}
printf("\nString in Reverse Order\n");
i = 0;
while(str[i] != '\0')
{
if(str[i] == ' ' || str[i] == '\n')
{
for(j = i - 1; j >= 0 && str[j] != ' '; j--)
printf("%c", str[j]);
}
i++;
}
printf("\n");
return 0;
}
For this input:
3
Enter String
string
written
backwards
this is output:
String in Reverse Order
gnirts
nettirw
gnirts
sdrawkcab
nettirw
gnirts

Ok I got there and now program is working perfectly. Thanks for tips!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
int i, j, n;
char str[100]="\0";
char buff[100];
scanf("%d", &n);
printf("Enter String\n");
for(int i=0;i<=n; i++)
{
fgets(buff, sizeof buff, stdin);
strcat(str, buff);
}
printf("\nString in Reverse Order\n");
i = 0;
while(str[i] != '\0')
{
if(str[i] == ' ' || str[i] == '\n')
{
for(j = i - 1; j >= 0 && str[j] != ' ' && str[j] != '\n'; j--)
printf("%c", str[j]);
printf("\n");
}
i++;
}
printf("\n");
return 0;
}

Related

Array changing its first letter without reason

I'm making a hangman and everything works fine if you win, but if you lose it doesn't print the word it was.
I was debugging it and I found that in one of the last iterations the first character is changed to '\000' so that is why it doesn't print, but I don't understand why because there is no line where the array word is changed.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define VOCABULARY_SIZE 8
#define MAX_STRING 32
#define MAX_GUESS 6
int random_number(int a, int b)
{
return a + rand() % (b - a + 1);
}
int main()
{
//random word selection
srand((unsigned)time(NULL));
const char VOCABULARY[VOCABULARY_SIZE][MAX_STRING] = {"vehicle", "building", "shirt", "pencil", "batman", "dromedary", "peach", "hangman"};
char word[MAX_STRING];
int i;
i = random_number(0, VOCABULARY_SIZE - 1);
strcpy(word, VOCABULARY[i]);
//user word
int guesses = 0, length = strlen(word);
char letters[MAX_GUESS+1];
char input[MAX_STRING];
char temp_char;
char temp_input[MAX_STRING];
do
{
printf("\nYour entered letters are: ");
printf("%s", letters);
printf("\nYour letters found are: ");
for (int j = 0; j < length; j++)
{
if (word[j] == input[j])
{
printf("%c", word[j]);
}
else
{
printf("_");
}
}
printf("\n%d-letter word. %d out of %d failures. Enter a letter: ", length, guesses, MAX_GUESS);
scanf(" %c", &temp_char);
letters[guesses] = temp_char;
letters[guesses+1] = '\0';
for (int j = 0; j < length; j++)
{
if (word[j] == temp_char)
{
input[j] = word[j];
}
}
guesses++;
printf("\nWhat is the word to guess? ");
scanf(" %s", temp_input);
} while ((strcmp(input, word) != 0 && strcmp(temp_input, word) != 0) && guesses <= MAX_GUESS);
if (strcmp(input, word) == 0 || strcmp(temp_input, word) == 0)
{
printf("\nCongratulations, the word was %s!", word);
}
else
{
printf("\nBetter luck next time... The word was %s", word);
}
}
You're writing to letters out of bounds
char letters[MAX_GUESS+1];
int guesses = 0;
do {
//...
letters[guesses+1] = '\0'; // OOPS
//...
guesses++;
//...
} while (... && guesses <= MAX_GUESS);
Nicely written code, but errors in string management.
Initialize
First time printf("%s", letters); called, letters[] is junk. Initialize it.
// char letters[MAX_GUESS + 1];
char letters[MAX_GUESS + 1] = { 0 };
Too many guess
Off by 1, lower limit.
// ... && guesses <= MAX_GUESS);
... && guesses < MAX_GUESS);
Missing '\0'
strcmp(input, word) uses input as a string, yet it lacks a null character.
Flush
Best to insure output is seen before asking for input.
fflush(stdout); // add
scanf(" %c", &temp_char);
fflush(stdout); // add
scanf(" %s", temp_input);
Unbounded input
scanf(" %s", temp_input); is worse than gets(). Research fgets().
Perhaps more issues.

Checking if all elements in array are zero

I'm having trouble determining if two words entered are anagrams.
#include <stdio.h>
#include <string.h>
int main() {
char ch;
int letter_count[26] = {0};
int i;
int sum = 0;
printf("Enter first word: ");
do
{
scanf("%c", &ch);
letter_count[ch - 'a']++;
} while (ch != '\n');
for(i = 0; i < 26; i++)
printf("%d ", letter_count[i]);
printf("\n");
printf("Enter second word: ");
do
{
scanf("%c", &ch);
letter_count[ch - 'a']--;
} while (ch != '\n');
for(i = 0; i < 26; i++)
printf("%d ", letter_count[i]);
for(i = 0; i < 26; i++)
if(letter_count[ch] != 0)
sum++;
if (sum == 0)
printf("anagrams");
else
printf("not anagrams");
}
I have to use the do while part of the code. I can enter the two words, and it prints out the elements in the array, so that "Mattress" and "Smartest" together would have all the elements be zero. However, I'm having trouble with the last part, which is to use a third loop to check whether all the elements are zero.
I figured I could declare an int before hand and have it increment whenever an element wasn't zero, and I could just have any sum greater than zero not be an anagram. However, it always prints out anagram for me.
In your third loop, using letter_count[ch] will not check the entire array. You should iterate through the array using the loop variable i. That part of the code should be:
for (i=0; i<26; i++)
if (letter_count[i] != 0)
sum++;
To handle both upper case and lower case letters, use topper() or to lower() in <ctype.h> to avoid out-of-bound access.
#include <stdio.h>
#include <string.h>
#include <ctype.h> // <---
int main() {
char ch;
int letter_count[26] = {0};
int i;
_Bool bad = 0;
printf("Enter first word: ");
do
{
scanf("%c", &ch);
if(!isalpha(ch)) // <---
{
puts("Not a letter");
continue;
}
letter_count[tolower(ch) - 'a']++; // <---
} while (ch != '\n');
for(i = 0; i < 26; i++)
printf("%d ", letter_count[i]);
printf("\n");
printf("Enter second word: ");
do
{
scanf("%c", &ch);
if(!isalpha(ch)) // <---
{
puts("Not a letter");
continue;
}
letter_count[tolower(ch) - 'a']--; // <---
} while (ch != '\n');
for(i = 0; i < 26; i++)
printf("%d ", letter_count[i]);
printf("\n"); // <---
for(i = 0; i < 26; i++)
if(letter_count[i] != 0)
{
bad = 1;
break; // <---
}
if (bad == 0)
printf("anagrams");
else
printf("not anagrams");
}
Take a look at all places marked // <---.

Char Count in C?

So I am getting 1 more extra character than I should when i compile this code, someone please tell me the stupid error i am doing.
#include <stdio.h>
#include <conio.h>
int main() {
char str[20];
char A = 'a';
char B = 'A';
int count = 0, i;
printf("Enter a string : ");
fgets(str, 20, stdin);
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == A||B)
count++;
}
if (count == 0)
printf("\nCharacter A is not present");
else
printf("\nThere are %d A's in your string.", count);
getchar();
return 0;
}
/* This is the issue (str[i] == A)||(str[i] == B) */
#include <stdio.h>
#include <conio.h>
int main() {
char str[20];
char A = 'a';
char B = 'A';
int count = 0, i;
printf("Enter a string : ");
fgets(str, 20, stdin);
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == A || str[i] == B)
count++;
}
if (count == 0)
printf("\nCharacter A is not present");
else
printf("\nThere are %d A's in your string.", count);
getchar();
return 0;
}

I can't read the second string, no matter how many getchar i insert

This program need to read two strings, this two strings will be passed to the "confirm" function, they will be read and the the function will have to find a word in common.
But in the main i cant read the "string2" string! No matter how many getchar i insert.
#include <stdio.h>
#include <string.h>
void confirm(char string1[],char string2[]){
char word[20];
char w_aux[20];
int i, j, k, l, m;
int len1, len2;
int find;
i = 0;
len1 = strlen(string1);
len2 = strlen(string2);
while (i < len1){
j = 0;
while ((string1[i] != ' ') && (i < len1)){
word[j] = string1[i];
i++;
j++;
}
word[j] = '\0';
k = 0;
find = 0;
while((k < len2) && (find == 0)){
l = 0;
while ((string2[k] != ' ') && (k < len2)){
w_aux[l] = string2[k];
k++;
l++;
}
w_aux[l] = '\0';
if (j == l){
m = 0;
while((word[m] == w_aux[m]) && (m<j)){
m++;
}
if (m == j){
find = 1;
}
}
k++;
}
i++;
}
printf("%s\n", word);
}
int main (){
char string1[20];
char string2[20];
printf("Type the first text: \n");
scanf("%[^\n]s", string1);
printf("Type the second text: \n");
scanf("%[^\n]s", string2);
getchar();
confirm(string1, string2);
return 0;
}
Use getchar after input of string 1 for \n:
printf("Type the first text: \n");
scanf("%[^\n]", string1);
getchar(); //here
printf("Type the second text: \n");
scanf("%[^\n]", string2);
Just change the format specifier to "%19[^\n]*c", it will take the trailing '\n' character away.

How to calculate blank space

i have been trying calculate the amount of blank space in a sentence, but I don't know how to let the program know which sentence that I want it to calculate, please help me, here is my code.
#include <stdio.h>
#include <string.h>
#include <conio.h>
int wid, len, i, j, temp, blank;
char text[100], ch;
int main(){
printf ("Enter the width of the colum: ");
gets (text);
sscanf (text, "%d", &wid);
printf ("\nEnter a line of text: ");
gets (text);
len = strlen (text);
for (i = 0; i < 7; i++){
printf ("1234567890");
}
while(len > 50){
printf ("\nThe text is too long!\n");
break;
}
ch = getch();
for (j = 0; j < len; j++){
if (ch == ' ') {blank++;}
}
printf ("\n%d", blank);
}
I want to calculate how many blank for this part
printf ("\nEnter a line of text: ");
gets (text);
instead of
ch = getch();
for (j = 0; j < len; j++){
if (ch == ' ') {blank++;}
try
for(j = 0; j < len; j++)
{
if(text[j] == ' ') //comparing if the string in text contains blank space at position j
blank++;
}
also, instead of gets() to read the string, use fgets.
/* cprogram to count blanck spaces in the string*/
#include<stdio.h>
#include<string.h>
int main()
{
int len,i,blank_space=0;
char str[20];
printf("Enter the string ");
fgets(str,20,stdin);
printf("%s",str);
//claculate string length
len=strlen(str);
//printing length of the string
printf("length of the string is %d",len);
//loop to detect blankspaces
for(i=0;i<len;i++)
{
if(str[i]==' ')
blank_space++;
}
//display the result
printf("no of blanked spaces is %d",blank_space);
return 0;
}

Resources