program to reverse string skipping some characters while printing - arrays

It prints the characters in reverse order just fine except when the string is 8 characters long.
Eg -
"what man" gives "am tahw" Why ?
whereas "what many" gives "ynam tahw" just as it should.
#include <stdio.h>
int main(void)
{
char a[100];
char x;
char*i = a;
printf("Enter a message:");
while ((x = getchar()) != '\n')
{
*i = x;
i++;
}
while (i >= &a[0])
{
printf("%c", *i--);
}
printf ("\n");
}

Modification in your code:
Change printf("%c", *i--); to printf("%c", *--i); in while loop.
You can improve the quality of your program as shown below:
#include <stdio.h>
#include <conio.h>
void main()
{
char *s;
int len,i;
clrscr();
printf("\nENTER A STRING: ");
gets(s);
len=strlen(s);
printf("\nTHE REVERSE OF THE STRING IS:");
for(i=len;i>=0;i--)
printf("%c",*(s+i));
getch();
}

Related

how to see if a letter char *c exist in word char s[30]?

here is code and i need help to find the place of letter.The strcmp is not working and i dont now where is the proble to fix.
#include <string.h>
int main(void)
{
char s[30]="fiordi";
char *c;
int cp,i,place;
printf("Enter char: ");
scanf("%s",&c);
for(i=0; i<6; i++){
cp=strcmp(s[i],c);
if( cp == 0 ){
place=i;
}
}
printf("the place is :%d",place);
}
#include <string.h>
#include <stdio.h>
int main(void)
{
char s[30]="fiordi";
size_t s_len = strlen(s);
int place, c;
printf("Enter char: ");
scanf("%c",&c);
for(place = 0; place < s_len; place++)
if(s[place] == (char)c)
break;
if(place == s_len)
printf("\nchar not found in string\n");
else
printf("the place of char in \"%s\" is in position %d", s, place);
}
strcmp compares strings, not characters. Without going into the details of why it doesn't work, you could just compare the characters directly:
if(s[i] == c[0] ){
place=i;
}

Find a palindrome words in a string and then rewrite them, in C

Hi, how can i write a code in C that checks a string for palindromes, and then rewrites them?
For example: string> "awbiue abdba aebto leoel", should return "abdba leoel".
I wrote this code, but it can only find that the string is palindrome or not:
#include<stdlib.h>
#include<string.h>
int main()
{
char str[100];
printf("Enter string: ");
gets(str);
int f=1;
{
for(int i=0;i<strlen(str); i++)
{
if(str[i]!=str[strlen(str)-i-1])
{
f=0; break;
}
}
if(f==1)
printf("Palindrom");
else
printf("Not Palindrom");}
return 0;
}
You only need to read string by string, making sure whether they are palindrome, and if so, print them out -- that is what I did in the following code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[100];
printf("Enter string: ");
while(scanf("%s", str) == 1) { // read strings one by one in the str variable
//your code part
int f=1;
for(int i=0;i<strlen(str); i++)
{
if(str[i]!=str[strlen(str)-i-1])
{
f=0; break;
}
}
if(f==1) // that means that string is palindrome
printf("%s ", str); // print the string and a space
}
return 0;
}

I'm trying to count the numbers in a string in C

What's wrong with my code? I'm trying to count the numbers in a string after skipping all alphabets.
How to skip the alphabets and only count the numbers?
#include <stdio.h>
#include <stdlib.h>
void function(char a[])
{
int i=0,count=0;
while(a[i]!='\0')
{
if(a[i]>='a'&&a[i]<='z')
{
continue;
}
else
{
count++;
}
i++;
}
printf("%d",count);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
char a[100001];
scanf("%s",a);
function(a);
printf("\n");
}
}
I switched your usage of scanf to use fgets. This works:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
void function(char a[])
{
int i=0,count=0;
while(a[i]!='\0')
{
if (isdigit(a[i]))
{
count++;
}
i++;
}
printf("%d",count);
}
int main()
{
int t = 0;
scanf("%d", &t);
while(t--)
{
char a[100001];
fgets(a, sizeof(a), stdin);
function(a);
puts("\n");
}
}
If the OP's intent was in fact to count up the groups of digits (in which the definition of 'number' is each contiguous run of digits), this will accomplish the goal:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int function(char a[])
{
size_t i = 0;
int cnt = 0;
// convert all non-digits to whitespace
printf("Converting\n");
for (char *p = a; *p != '\0'; ++p)
{
*p = isdigit(*p) ? *p : ' ';
}
printf("Found Groups: %s\n",a);
// note: strtok mutates the string it is given, it will not be usable after this:
for (char *gr = strtok(a," "); gr != NULL; gr = strtok(NULL, " "))
{
cnt++;
}
printf("Number of Groups: %d\n", cnt);
}
int main()
{
printf("Enter strings (Ctrl-C to end)\n");
while(1)
{
int result = 0;
char a[100001];
scanf("%s",a);
result = function(a);
printf("\n");
}
}
(I was not able to get the version using fgets to work, but how the string is gathered is not really in the scope of the question, the OP's original code in main is functional, I just made it an open-ended loop for my tests)
In your code continue statement is responsible for wrong answer. Because of it your code is not encountering the statement a[i] !='\0'. so suggest you to just avoid it and close your if statement without any code as like me. One more suggestion use also uppercase letters as user can also enter uppercase letters and using fgets() function you can also read white spaces.
Try out this
#include <stdio.h>
#include <stdlib.h>
void function(char a[])
{
int i=0,count=0;
while(a[i]!='\0')
{
if((a[i]>='a'&&a[i]<='z') || (a[i]>='A'&&a[i]<='Z'));
else
{
count++;
}
i++;
}
printf("%d",count);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
char a[100001];
scanf("%s",a);
function(a);
printf("\n");
}
}

How to remove multiple trailing newlines after fgets()?

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main()
{
int upper=0;
int digit=0;
int i=0;
char s[30];
char c;
printf("Enter sentence: ");
fgets(s, 30, stdin);
//s[strlen(s) - 1] = '\0';
while(c=getchar() && c!='\n')
{
c = s[i];
if(isupper(c))
{
upper++;
}
if(isdigit(c))
{
digit++;
}
i++;
}
printf("Number of upper case letters............... %d", upper);
printf("\n");
printf("Number of digits........................... %d", digit);
printf("\n");
printf("Program done. ");
return 0;
system("PAUSE");
}
How can I remove the multiple newlines after the fgets() ?
I have tried implementing the following line after fgets()
s[strlen(s) - 1] = '\0';
but this does not work and my program does not run through all the code.
Without the code -----> s[strlen(s) - 1] = '\0';
Here is the Output:
Enter sentence: Whats UP 1234
Number of upper case letters............... 3
Number of digits........................... 4
Program done.
Process returned 0 (0x0) execution time : 9.340 s
Press any key to continue.
As you can see my program is able to run however I have to press enter multiple times and there is a lot of newlines
and then at the very end the program runs the last bit of code.
The program is suppose to count the number of upper case letters and digits in the string
entered. Can someone please explain why this is happening ?
Thanks to Jongware:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main()
{
int upper=0;
int digit=0;
int i=0;
char s[30];
char c;
printf("Enter sentence: ");
fgets(s, 30, stdin);
//correction: getchar() has been removed from the while condition.
while( c!='\n')
{
c = s[i];
if(isupper(c))
{
upper++;
}
if(isdigit(c))
{
digit++;
}
i++;
}
printf("Number of upper case letters............... %d", upper);
printf("\n");
printf("Number of digits........................... %d", digit);
printf("\n");
return 0;
system("PAUSE");
}

C programming Hex to Char

What I'm trying to do is have the user input a hex number this number will then be converted to a char and displayed to the monitor this will continue until an EOF is encountered.I have the opposite of this code done which converts a char to a hex number. The problem I'm running into is how do i get a hex number from the user I used getchar() for the char2hex program. Is there any similar function for hex numbers?
this is the code for the char2hex program
#include <stdio.h>
int main(void) {
char myChar;
int counter = 0;
while (EOF != (myChar = getchar())) {
/* don't convert newline into hex */
if (myChar == '\n')
continue;
printf("%02x ", myChar);
if (counter > 18) {
printf("\n");
counter = -1;
}
counter++;
}
system("pause");
return 0;
}
this is what i want to the program to do except it would do this continuously
#include <stdio.h>
int main() {
char myChar;
printf("Enter any hex number: ");
scanf("%x", &myChar);
printf("Equivalent Char is: %c\n", myChar);
system("pause");
return 0;
}
any help would be appreciated
thank you
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void) {
int myChar;
int counter = 0;
char buff[3] = {0};
while (EOF != (myChar = getchar())) {
if(isxdigit(myChar)){
buff[counter++] = myChar;
if(counter == 2){
counter = 0;
myChar = strtol(buff, NULL, 16);
putchar(myChar);
}
}
}
printf("\n");
system("pause");
return 0;
}
Because chars and ints can be used interchangably in C, you can use the following code:
int main(void) {
int myChar;
printf("Enter any hex number: ");
scanf("%x", &myChar);
printf("Equivalent Char is: %c\n", myChar);
system("pause");
return 0;
}
If you want it to loop then just enclose it in the while loop as in your example code.
Edit: You can try out the working code here http://ideone.com/yyvz85

Resources