Exit program in string functions - upper to lower characters and vice versa - c

I wanted the two functions to stop the program as the word "exit" is entered, but the loop for that specification is not working. can you please fix this?
Here is the code. please check it.
#include<stdio.h>
#include<string.h>
void uppercase(char *str) {
int i;
for(i=0; i<=strlen(str); i++) {
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\nThe string in lower case is->%s\n",str);
}
void lowercase(char *str) {
int i;
for(i=0; i<=strlen(str); i++) {
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("The uppercase equivalent is: %s\n", str);
getchar();
}
int main()
{
char str[100];
char lower[100];
int i;
while(1)
{
printf("Enter any string->");
scanf("%s",&str);
if (str == 'exit')
{
break;
}
else
{
printf("The string is->%s\n",str);
uppercase(str);
lowercase(str);
}
}
return 0;
}

replace:
if (str == 'exit')
by this
if (strcmp(str , "exit")==0)

void uppercase(char *str){
int i;
for(i=0;i<strlen(str);i++){
//if(islower(str[i]))
str[i]=toupper(str[i]);
}
printf("\nThe string in uppercase is->%s\n",str);
}

Related

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 want to change the output value of the string

#include <stdio.h>
int main() {
char str[101];
int i;
int j=1;
scanf("%s", str);
for(i=0; str[i]!='\0'; i++) {
if(i!=0 && i%j==0) {
printf("\n");
j++;
}
printf("%c", str[i]);
}
}
If I input "abcdefg" in this code, I want it printed in turn like stairs.
a(\n)
bc(\n)
def(\n)
g
How fix code?
Try:
#include <stdio.h>
int main() {
char str[101];
int i;
int j=1,k=0;
scanf("%s", str);
for(i=0;str[i]!='\0';i=k+i)
{
for(j=i;j<=i+k && str[j]!='\0';j++)
printf("%c", str[j]);
k++;
if(str[j]!='\0')
printf("\n");
}
}
This could fix your problem
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j=1,k;
char str[100];
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
for(k=0;k<j;k++)
{
if(str[k+i]!='\0')
printf("%c",str[k+i]);
else
exit(1);
}
printf("\n");
j++;
i=k+i-1;
}
return 0;
}

Trying to introduce blanks in regular interval in a string

I tried to write a function that inserts space at regular intervals in a string.
If a[50] is a string, and n is the preferred interval from the user,
insert_space(a,b,len,n) will insert blanks after the n'th column and will store the modified string in b.
#include <stdio.h>
int getinput(char temp[]);
void insert_space(char s1[],char s2[],int,int);
int main ()
{
int n, len;
char a[100], b[100];
printf("Enter the nth column number for inserting\n");
scanf("%d",&n);
printf("Enter the line\n");
len=getinput(a);
insert_space(a,b,len,n);
printf("%s\n",b);
}
void insert_space(char s1[],char s2[],int len, int n)
{
int i=0, c=0,flag=0;
for(i=0;i<=len;i++)
{
if(flag!=n)
{
s2[c]=s1[i];
c++;
flag++;
}
else
{
s2[c]=' ';
i=i-1;
c++;
flag=0;
}
}
s2[c]='\0';
}
int getinput(char temp[])
{
int c, i=0;
while((c=getchar())!=EOF)
{
temp[i]=c;
i++;
}
i--;
temp[i]='\0';
return i;
}
I entered the values of the string a as abcdefghijkmnop. Instead of
"abdce fghij kmnop" as the ouput in b, I got "abcd efghi jkmno p" as the output. I'm not sure what I did wrong here.
edit: After just including the insert_function code, I've edited it to include the entire execution code.
There is a \n ,newline (Enter) from scanf("%d",&n); which is recorded as a[0]. So you have to manage this UN-handled newline (Enter).
To solve this, add an extra c = getchar(); before loop while ((c = getchar()) != EOF) in function int getinput(char temp[]), to handle that extra newline left behind by scanf("%d",&n);
Modified code:-
#include <stdio.h>
int getinput(char temp[]);
void insert_space(char s1[], char s2[], int, int);
int main()
{
int n, len;
char a[100], b[100];
printf("Enter the nth column number for inserting\n");
scanf("%d", &n);
printf("Enter the line\n");
len = getinput(a);
insert_space(a, b, len, n);
printf("%s\n", b);
}
void insert_space(char s1[], char s2[], int len, int n)
{
int i = 0, c = 0, flag = 0;
for (i = 0; i <= len; i++)
{
if (flag != n)
{
s2[c] = s1[i];
c++;
flag++;
}
else
{
s2[c] = ' ';
i = i - 1;
c++;
flag = 0;
}
}
s2[c] = '\0';
}
int getinput(char temp[])
{
int c, i = 0;
c = getchar(); // to handle extra newline from scanf
while ((c = getchar()) != EOF)
{
temp[i] = c;
i++;
}
i--;
temp[i] = '\0';
return i;
}
Output :-
Enter the nth column number for inserting
5
Enter the line
abcdefghijkmnop
abcde fghij kmnop

Palindrome program in C

My program in C which is Palindrome has an error in its function. My function is not comparing the 2 characters in my string. When I type a single character it answers palindrome but if it is two or more always not palindrome.
Code:
int IntStrlength=strlen(StrWord);
int IntCtr2=0;
int IntCtr=1, IntAnswer;
while(IntCtr<=(IntStrlength/2)){
printf(" %d %d\n", IntCtr2,IntStrlength);
if(StrWord[IntStrlength] != StrWord[IntCtr2]){
IntAnswer=0;
printf(" %d=Not Palindrome", IntAnswer);
exit (0);
}//if(StrWord[IntCtr2]!=StrWord[IntStrlength]) <---------
else{
IntCtr2++;
IntStrlength--;
}// else <--------
IntCtr++;
}//while(IntCtr<IntStrlength/2) <-----------
IntAnswer=1;
printf(" %d=Palindrome", IntAnswer);
return ;
}
Single character:
Two or more characters:
Why not write it like this
int wordLength = strlen(StrWord);
for (int i=0;i<(wordLength/2);i++) {
if (StrWord[i] != StrWord[wordLength-i-1]) {
return 0;
}
}
return 1;
For words with an even length (say 8) the counter will go from 0 to 3, accessing all letters. For uneven words (say 7) the c ounter will go from 0 to 2, leaving the middle element unchecked. This is not necessary since its a palindrome and it always matches itself
#include<stdio.h>
int check_palindrom(char *);
int main()
{
char s1[20];
printf("Enter the string...\n");
gets(s1);
int x;
x=check_palindrom(s1);
x?printf("Palindrom\n"):printf("Not Palindrom\n");
}
int check_palindrom(char *s)
{
int i,j;
for(i=0;s[i];i++);
for(i=i-1,j=0;i>j;i--,j++)
if(s[i]!=s[j])
return 0;
if(s[i]==s[j])
return 1;
}
Enter the string...
radar
Palindrom
I've seen this algorithm before in a interview book called "Cracking the Coding Interview".
In it the author shows a very simple and easy implementation of the code. The code is below: Also here is a video explaining the code.
#include<stdio.h>
#include<string.h> // strlen()
void isPalindrome(char str[]);
int main(){
isPalindrome("MOM");
isPalindrome("M");
return 0;
}
void isPalindrome(char str[]){
int lm = 0;//left most index
int rm = strlen(str) - 1;//right most index
while(rm > lm){
if(str[lm++] != str[rm--]){
printf("No, %s is NOT a palindrome \n", str);
return;
}
}
printf("Yes, %s is a palindrome because the word reversed is the same \n", str);
}
You can do this like this:
#include <stdio.h>
#include <string.h>
int check_palindrome(char string []);
int main()
{
char string[20];
printf("Enter the string...\n");
scanf ("%s", &string);
int check;
check = check_palindrome (string);
if (check == 0)
printf ("Not Palindrome\n");
else
printf ("Palindrome\n");
return 0;
}
int check_palindrome (char string [])
{
char duplicate [];
strcpy (string, duplicate);
strrev (string);
if (strcmp (string, duplicate) == 0)
return 1;
else
return 0;
}
This uses the strcmp and strrev function.
Take a look at this code, that's how I have implemented it (remember to #include <stdbool.h> or it will not work):
for(i = 0; i < string_length; i++)
{
if(sentence[i] == sentence[string_lenght-1-i])
palindrome = true;
else
{
palindrome = false;
break;
}
}
Doing that it will check if your sentence is palindrome and, at the first occurence this is not true it will break the for loop. You can use something like
if(palindrome)
printf(..);
else
printf(..);
for a simple prompt for the user.
Example :
radar is palindrome
abba is palindrome
abcabc is not palindrome
Please , pay attention to the fact that
Abba
is not recognized as a palindrome due to the fact that ' A ' and 'a' have different ASCII codes :
'A' has the value of 65
'a' has the value of 97
according to the ASCII table. You can find out more here.
You can avoid this issue trasforming all the characters of the string to lower case characters.
You can do this including the <ctype.h> library and calling the function int tolower(int c); like that :
for ( ; *p; ++p) *p = tolower(*p);
or
for(int i = 0; str[i]; i++){
str[i] = tolower(str[i]);
}
Code by Earlz, take a look at this Q&A to look deeper into that.
EDIT : I made a simple program to do this, see if it can help you
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
void LowerCharacters(char *word, int word_lenth);
int main(void){
char *word = (char *) malloc(10);
bool palindrome = false;
if(word == 0)
{
printf("\nERROR : Out of memory.\n\n");
return 1;
}
printf("\nEnter a word to check if it is palindrome or not : ");
scanf("%s", word);
int word_length = strlen(word);
LowerCharacters(word,word_length);
for(int i = 0; i < word_length; i++)
{
if(word[i] == word[word_length-1-i])
palindrome = true;
else
{
palindrome = false;
break;
}
}
palindrome ? printf("\nThe word %s is palindrome.\n\n", word) : printf("\nThe word %s is not palindrome.\n\n", word);
free(word);
return 0;
}
void LowerCharacters(char *word, int word_length){
for(int i = 0; i < word_length; i++)
word[i] = tolower(word[i]);
}
Input :
Enter a word to check if it is palindrome or not : RadaR
Output :
The word radar is palindrome.
This code may help you to understand the concept:
#include<stdio.h>
int main()
{
char str[50];
int i,j,flag=1;
printf("Enter the string");
gets(str);
for(i=0;str[i]!='\0';i++);
for(i=i-1,j=0;j<i;j++,i--)
{
str[i]=str[i]+str[j];
str[j]=str[i]-str[j];
str[i]=str[i]-str[j];
}
for(i=0;str[i]!='\0';i++);
for(i=i-1,j=0;j<i;j++,i--)
{
if(str[i]==str[j]){
flag=0;
break;
}
}if(flag==0)
{
printf("Palindrome");
}else
{
printf("Not Palindrome");
}
}
I have solution for this
char a[]="abbba";
int i,j,b=strlen(a),flag=0;
for(i=0,j=0; i<b; i++,j++)
{
if(a[i]!=a[b-j-1])
{
flag=1;
break;
}
}
if(flag)
{
printf("the string is not palindrum");
}
else
{
printf("the string is palindrum");
}
This may works for you
#include <stdio.h>
#include <stdlib.h>
int main(void) {
setbuf(stdout,NULL);
int i,limit;
char string1[10];
int flag=0;
printf("enter a string");
scanf("%s",string1);
limit=strlen(string1);
for(i=0;i<limit;i++){
if(string1[i]!=string1[limit-i-1]){
flag=1;
break;
}
} if(flag==1){
printf("entered string is not palindrome");
}else{
printf("entered string is palindrome");
}
return EXIT_SUCCESS;
}

Arrays in a Palindrome program

So I made a program where I have to input a word and it displays if it is a palindrome (a word that is the same both ways) or not.
#include <stdio.h>
#include <string.h>
int main(int argc, const char * argv[]){
char word;
int length, counter;
printf("Please enter a word: ");
scanf("%c", &word);
int flag = 1;
for (counter = 0; counter < length && flag; counter++) {
printf("%c\t %c", word[counter], word[length - counter])
if (word[counter] == word[length - counter - 1]){
flag = 0;
}
}
if (flag) {
printf("%c is a palindrome!", word);
}
else {
printf("%c is NOT a palindrome!", word);
}
}
I set it up so that it displays each letter side by side. If a letter isn't the same then the flag is "thrown"(set to 0) and this will end the program saying: "word is NOT a palindrome!"
I get an error at the part where it says word[counter] saying it isn't a subscripted value. What can I do to make this work? Is there anything else I am doing wrong?
This char word; is not an array. This char word[100]; is an Array. Also you read a single character using scanf("%c", &word); not a word (as in a string or series of characters). Use:
fgets (word , 100 , stdin)
Also length is not initialized, so it will lead to UB.
Make this modifications in your program.It will run fine.
#include <stdio.h>
#include <string.h>
int main()
{
char word[100];
int length, counter;
printf("Please enter a word: ");
scanf("%s",word);
length=strlen(word);
int flag = 1;
for(counter = 0; counter < length/2 && flag; counter++)
{
if (word[counter] != word[length-counter-1])
{
flag = 0;
break;
}
}
if (flag)
{
printf("%s is a palindrome!\n", word);
}
else {
printf("%s is NOT a palindrome\n!", word);
}
}
****************************************************************
* Simple Array Palindrome Program *
****************************************************************/
#include <iostream>
using namespace std;
int main (){
int arr_size;
int flag=0;
/*****************************************
* Array Size *
*****************************************/
cout<<"Enter The Array Size: \n->arr[";
cin>>arr_size;cout<<" ]";
int arr[arr_size];
/*****************************************
* User_Input *
*****************************************/
for(int i=0;i<arr_size;i++){
cout<<"Enter Value For Arr[ "<<i<<" ] -> ";
cin>>arr[i];
}
/*****************************************
* Palindrome_Check *
*****************************************/
for(int k=0,j=arr_size-1;k<arr_size && j>-1;k++)
{
if(arr[i]==arr[j];
{
flag++;
}
}
/*****************************************
* Flag Check *
*****************************************/
if(flag==arr_size) {
cout<<"Array Is Palindrome: ";
}
else
{
cout<<"Array Is Not Palindrome: ";
}
}

Resources