Palindrome Checker in C - c

Where should I include toupper() in my code in order to make a palindrome such as Noon or NoOoON to say it is a palindrome rather than saying it is not a palindrome. I can't seem to figure it out. Thanks.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void reverse(char s[]){
int c, i , j;
for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
return;
}
int main(){
char a[20];
char b[20];
printf("Enter a string:\n");
gets(a);
strcpy(b,a); // copies string a to b
reverse(a); // reverses string b
if(strcmp(a, b) == 0) { // compares if the original and reverse strings are the same
printf("The string is a Palindrome\n");
}
else {
printf("The string is not a Palindrome\n");
}
return 0;
}

In your case, you can just use _stricmp instead of strcmp.
Another way to approach this is to convert your string to a single case after it is input. eg
for (char *c = a; *c; c++) *c = toupper(*c);

If you want to use toupper() then you should apply it before you make a copy of the string and reverse it.
That is:
int main() {
char a[20];
char b[20];
int i = 0;
printf("Enter a string:\n");
gets(a);
// make the change here
for (i = 0; i < strlen(a); i++) {
a[i] = toupper(a[i]);
}
strcpy(b, a);
If you convert the string to a single case later, then the copy will not be the same as the original, or you'd have to toupper() both.

Related

A program for inverting strings in c using pointers and bubble sort

I have to write a program that takes a string input and inverts it, so basically reverses the text.
I also wanted to try and reuse one of my codes that had bubble sort and thought it could work.
So this is my code:
#include <stdio.h>
int main(){
int n;
char arr[n];
printf("Input length:\n");
scanf("%d",&n);
printf("Input string:\n");
scanf("%s",&arr);
bsort(arr);
printf("%s",arr);
}
void swap(char *x,char *y){
char temp=*x;
*x=*y;
*y=temp;
}
void bsort(char *arr, int n){
int i, j;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(&arr[j]<&arr[j+1]){
swap(arr[j],arr[j+1]);
}
}
}
}
I don't know if I messed up data types, operators, or the functions altogether.
When I run the program, there's nothing printed out.
I'd appreciate any suggestions.
#include <stdio.h>
#include <string.h>
void reverseString(char* str)
{
int l, i;
char *beginPtr, *endPtr, ch;
// Get the length of the string using this func instead of aking input
l = strlen(str);
// Setting the beginPtr
// to start of string
beginPtr = str;
//Setting the endPtr the end of
//the string
endPtr = str + l - 1;
//basically we add the (len-1)
// Swap the char from start and end
// index using beginPtr and endPtr
for (i = 0; i < (l - 1) / 2; i++) {
// swap character
ch = *endPtr;
*endPtr = *beginPtr;
*beginPtr = ch;
// update pointers positions
beginPtr++;
endPtr--;
}
}
// Driver code
int main()
{
// Get the string
char str[100];
printf("Enter a string: ");
scanf("%s",&str);
// Reverse the string
reverseString(str);
// Print the result
printf("Reverse of the string: %s\n", str);
return 0;
}
I have tried to explain this code using comments
if u dont wanna use string.h then u can ask input for length of the string

Junk values while reversing a string?

My program is to reverse, even though the reverse is being generated but the problem is there is an unwanted junk values too.
I am not able to understand where the problem is.
#include <stdio.h>
#include<string.h>
int main()
{
char ar[100],b[100];
int i,j;
scanf("%s",ar);
j=strlen(ar);
printf("%d",j);
j-=1;
for(i=0;j>=0;i++)
{
b[i]=ar[j];
j--;
}
printf("\n %s",b);
}
This is the output:
You need to add
b[i] = 0;
at the end to terminate the string.
The function printf() depends on the NUL-terminating character as a marker to stop printing, so you should terminate your array with the character '\0'. Also it would be better to make a function to reverse a string:
#include <stdio.h>
#include <string.h>
void m_strrev(char *str, char *output);
int main(void)
{
char ar[100], b[100];
//int i, j;
scanf("%s", ar);
/*j = strlen(ar) - 1;
for (i = 0; j >= 0; i++)
{
b[i] = ar[j];
j--;
}
b[i] = '\0';
printf("%s\n", b);*/
m_strrev(ar, b);
printf("%s\n", b);
}
void m_strrev(char *str, char *output)
{
char *e = str;
while (*e) {
e++;
}
e--;
while (e >= str) {
*output++ = *e--;
}
*output = '\0';
}

Code to Reverse String not clearing up previous values

I am doing this programming problem where I have reverse string of about 30 characters for 10 test cases.
My code is this:-
#include <stdio.h>
#include <string.h>
int main () {
int t;
scanf("%d",&t);
while (t--) {
char str[30];
scanf("%s",&str);
char revStr[30];
int len = strlen(str);
int i = 0;
int j = len-1;
while (i < len) {
revStr[i] = str[j];
i++; j--;
}
printf("%s\n",revStr);
}
return 0;
}
The output gets garbled up if the input string is larger than previous string.
For example,
if last-string had 6 characters, like rocket\0 and new-string, which is fun\0 has 3 characters, the output is funket\0.
char str[30];
scanf("%s",&str);
^ don't pass address of array
Just this would work -
scanf("%29s",str);
Try this:
int t;
scanf("%d", &t);
while (t--)
{
char str[30] = { 0 };
scanf("%s", &str);
char revStr[30] = { 0 };
int len = strlen(str);
int i = 0;
int j = len - 1;
while (i < len) {
revStr[i] = str[j];
i++; j--;
}
printf("%s\n", revStr);
}
You need to make two changes
Firstly change scanf("%s",&str); to
scanf("%s",str);
Secondly, after the while loop, you are not making the last element rev string \0. Add this line before the printf statement.
revStr[i] = '\0';
This should solve your problem.

C program for how to reverse a number explanation needed

While searching for a C program on how to reverse a string, i came across the below program. I am already familiar with a program where we take the length of the string and then minus each character and find the reverse. But this a different program. So can someone please tell me how this code works? Help will be gratefully accepted.
compiler used is Borland Turbo c.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main() {
char str[50];
char rev[50];
int i, j, n;
clrscr();
printf("enter the string to be reversed:");
scanf("%s", &str);
for (i = 0; str[i] != 0; i++) {
n = i - 1;
}
for (j = 0; j <= i - 1; j++) {
rev[j] = str[i];
n--;
}
printf("the reverse of the string is:%s", rev);
getch();
}
In C, strings are NUL-terminated meaning that it has '\0' at the end signifying the end of the string. The code you've posted currently has two issues:
This:
scanf("%s", &str);
should be
scanf("%s", str);
as %s expects a char*, not a char(*)[50].
This:
rev[j] = str[i];
should be
rev[j] = str[n];
rev should be NUL-terminated before printing. Add
rev[j] = '\0';
just before
printf("the reverse of the string is:%s", rev);
to avoid Undefined Behavior.
Your code doesn't work as it is supposed to.Have you tried it ?
Consider this approach to reverse a string :
#include <stdio.h>
#include <string.h>
int reverse(char *Str);
void swap(char *x, char *y);
int main(int argc, char *argv[]) {
char Str[255];
printf("enter the string to be reversed : ");
scanf("%s", Str);
reverse(Str);
printf("the reverse of the string is : %s\n", Str);
}
int reverse(char *Str) {
size_t len = strlen(Str);
size_t n = len / 2;
char *begin = Str;
char *end = (Str + len) - 1;
while (n > 0) {
swap(begin, end);
begin++;
end--;
n--;
}
return 0;
}
void swap(char *x, char *y) {
char tmp;
tmp = *x;
*x = *y;
*y = tmp;
}

string reverse program in C

i have written a program to reverse a string.. But it is not working.. It is printing the same string which is scanned.. What is the problem with the code?
#include <stdio.h>
#include <stdlib.h>
char *strrev(char *s)
{
char *temp = s;
char *result = s;
char t;
int l = 0, i;
while (*temp) {
l++;
temp++;
}
temp--;
for (i = 0; i < l; i++) {
t = *temp;
*temp = *s;
*s = t;
s++;
temp--;
}
return result;
}
int main()
{
char *str;
str = malloc(50);
printf("Enter a string: ");
scanf("%s", str);
printf("%s\n\n", strrev(str));
return 0;
}
for (i = 0; i < l; i++)
You're walking through the entire string, so you're reversing it twice - it won't be reversed after all. Walk only halfways:
for (i = 0; i < l / 2; i++)
Also, try using int len = strlen() instead of the while-not-end-of-string loop, if you're permitted to do so.
You swap the string's content twice.
Use the following code ..
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
char *strrev(char *s)
{
char *temp = s;
char *result = s;
char t;
while (*temp)
temp++;
while (--temp != s)
{
t = *temp;
*temp = *s;
*s++ = t;
}
return result;
}
int main()
{
char *str;
str = (char*)malloc(50);
printf("Enter a string: ");
scanf("%s", str);
printf("%s\n\n", strrev(str));
return 0;
}
The logic is to swap characters from start upto first half with the characters from last of second half, i.e, upto len/2. Just modify your for loop as below & it will work fine for you
for (i = 0; i < l/2; i++) {
you can use this simple code
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int str_len (char *str)
{
char *ptr = str;
while (*str)
str++;
return str - ptr;
}
int main ()
{
char *str;
int length;
str = (char*)malloc(50);
printf("Enter a string: ");
scanf("%s", str);
length = str_len(str) - 1;
for (int i = length ; i >= 0 ; i--)
printf ("%c", str[i]);
return 0;
}
you can use this code to reverse the string
#include<stdio.h>
#include<string.h>
int main()
{
int n,i;
char str2[100],str1[100];
printf("enter teh string 1\n");
gets(str1);
n = strlen(str1);
for(i=0;i<n;i++)
{
str2[n-1-i]=str1[i];
}
printf("%s\n",str2);
}
Actually you are reversing the string twice...so after come to middle of the string, you should terminate the loop that is your loop should be run for half of the string length that is l/2 (in this case). so your loop should be like
for(i = 0; i < i / 2; i++)
swapping the string content twice..
swapping it once will help..
for (i = 0; i < l/2; i++)

Resources