I've been assigned to construct a code, doing the following:
Ask the user to input a word (30 letters max), and then delete all the "A,a,O,o" letters and print it without any spaces. eg: input: OkleaAo, output: kle
I've been told the easier way is to use 2 arrays, and with an IF loop, to add letters which aren't A/a/o/O to the second array, and then print it.
I'm really stuck now, I know I have to use pointers, but for it isn't working.
Any help, advise?
I wrote something like this:
#include <stdio.h>
char input[30], *p;
char output[30];
main()
{
printf("Enter a string:\n", input);
scanf("%s", input);
if *(p==o);
{
*p=0;
printf("String after deletion:", input);
}
}
#include <stdio.h>
#include <string.h>
int main(){
char input[30+1];
char output[30+1];
int i, j;
printf("Enter a string :");
scanf("%30[^\n]", input);
for(j = i = 0;input[i];++i){
char *p, ch = input[i];
p = strchr("AaOo ", ch);
if(p == NULL)
output[j++] = ch;
}
output[j] = '\0';
printf("\nString after deletion: %s\n", output);
return 0;
}
#include <stdio.h>
int main(){
char input[30+1];
char *in, *out;
printf("Enter a string :");
scanf("%30[^\n]", input);
out = in = input;
while(*in){
char ch = *in;
if(ch != 'A' && ch != 'a' && ch != 'O' && ch != 'o' && ch != ' ')
*out++ = *in;
++in;
}
*out = '\0';
printf("\nString after deletion: %s\n", input);
return 0;
}
Related
Im trying to make the program remove a character from a string that the user is putting in but i get an error inside the loop. (side question: is adding a character inside the string the "same" code with some small changes?)
PS New to programming...
Is this what you are trying to achieve?
Changes:
getchar() and fgets() to scanf()
added strlen() to get length of string
added lenght of input string to printf
added zero init of strings
Note: After each input with scanf() you have to press enter.
int main()
{
char str[100] = { 0 };
char ch[5] = { 0 };
int k, j;
printf("Write text:\n");
scanf("%s", str);
printf("Input was: %s\nLength: %d\n", str, strlen(str));
printf("Write a character that should be removed\n");
scanf("%s", ch);
for (k = 0, j = 0; k < strlen(str); k++)
{
if (str[k] != ch[0]) {
str[j] = str[k];
j++;
}
}
str[j] = '\0';
printf("String after removing a character: %s", str);
}
Problems with your code:
str[k] != ch would be a valid test if ch were indeed a character and not an array of characters of length 5. This is going to compare the character value of str[k] with the address &ch[0].
k < str would be a valid comparison if k was a char * pointer that was initialized at &str[0], not an int loop index starting at 0.
Corrected code:
int main(void)
{
char str[100];
char ch[5];
int k, j;
printf("Write text:\n");
//getchar();
fgets(str, 100, stdin);
printf("Input was: %s\n", str);
printf("Write a character that should be removed\n");
//getchar();
fgets(ch, 5, stdin);
for (k = 0, j = 0; k < strlen(str); k++)
{
if (str[k] != ch[0])
{
str[j] = str[k];
j++;
}
}
str[j] = '\0';
printf("String after removing a character = %s", str);
return 0;
}
Here you have two implementations. Both remove all occurrences of the char ch from the string str
First algorithm is much faster. The second slower but easy to understand
#include <stdio.h>
#include <string.h>
char *removechar(char *str, int ch)
{
char *cptr = str, *readptr = str;
while(*readptr)
{
if(*readptr == ch)
{
readptr++;
}
else
{
*cptr++ = *readptr++;
}
}
*cptr = 0;
return str;
}
char *removechar(char *str, int ch)
{
char *cpos = str;
while((cpos = strchr(cpos, ch)))
{
strcpy(cpos, cpos + 1);
}
return str;
}
int main()
{
char s[] = "Hello World";
printf("%s\n", removechar(s, 'd'));
printf("%s\n", removechar(s, 'l'));
return 0;
}
Is this what you want to achieve?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char str[100],c;
printf("Write text:\n");
fgets(str,100,stdin);
printf("Input was: %s\n", str);
printf("Write a character that should be removed\n");
c=getchar();
for(int k=0;k<strlen(str);k++){
if(str[k]==c){
for(int j=k;j<strlen(str);j++){
str[j]=str[j+1];
}
}
}
printf("String after removing a character = %s", str);
return 0;
}
I have been trying to write a code and a part of it is to scan multiple strings until it gets the 'Q' string. In this case the scanf should stop.
How should I do it?
I have tried several ways, but none of them worked.
char array[100]={0};
while (flag == 1)
{
scanf("%s", array);
if(array == 'Q') {
flag=0;
}
}
You could do it using the strcmp() function in the string.h library.
#include <stdio.h>
#include <string.h>
int main() {
char array[100] = {0};
int flag = 1;
while (flag == 1) {
scanf("%s", array);
if (!strcmp(array, "Q"))
flag = 0;
}
return 0;
}
array[0] == 'Q'
#include <stdio.h>
int main(){
int flag = 1;
char array[100]={0};
while (flag == 1)
{
scanf("%s", array);
if(array[0] == 'Q') {
flag=0;
}
}
}
you can do this with this code
ch= getchar();
while( (ch != 'q' || ch !='Q') {
ch = getchar();
putc(ch, fp);
}
your mistake is herescanf("%s", array); it reads string and if condition you are checking without index of array variable
you can check also by reading value with `scanf("%s", array); by looping value of array to find q in array
char a[100];
int i;
scanf("%c",&a[0]);
i=1;
while(a[i]!='q'&& a[i]='Q')
{
scanf("%c",&a[i]);
i++;
}
This is what I would do, if you have to use scanf() (Notice the %99s to prevent buffer overflow -> 99 + '\0' == you buffer length)
#include <stdio.h>
int main()
{
char array[100]={0};
do
{
scanf("%99s", array);
} while (!(strlen(array) == 1 && array[0] == 'Q'));
}
Online C Compiler
How to pick a word in a text line in string in C language?
Example string "My mother cooks well...." How I can edit only "cooks" in that string? Question is for an exam. How can I find lenght and how can I edit second word in text, for example?
#include <stdio.h>
int length(char* s) // Lenght
{
int d = -1;
while (s[++d]);
return d;
}
int main() //main function
{
char str[101], c;
int i = 0;
printf("Entry text:\n");
scanf("%s", str); //Input text line
printf("First word lenght('%s') je %d.\n", str, lenght(str));
do
{
scanf("%c", &c);
str[i++] = c;
} while (c != '\n');
str[i - 1] = 0;
printf("The rest: '%s'\n", str); //Rest lenght
printf("The rest lenght: %d.", lenght(str));
return 0;
}
You can use strtok()
int i = 0;
char delim[2] = " ";
char *c = strtok(str, delim); //space is the delimiter.
// c points to the first word
while(c != NULL)
{
printf(" %s\n",c);
c = strtok(str, NULL) //notice this NULL
i++;
if(i == 2)
{
//edit your 2nd word
//break if you want after this or carry on
}
}
See strstr(3). I think that's all you need.
basically we are provided with the main function:
#include <stdio.h>
#include <string.h>
#include <strings.h>
#define STORAGE 255
{
int c;
char s[STORAGE];
for(;;) {
(void) printf("n=%d, s=[%s]\n", c = getword(s), s);
if (c == -1)break;
}}
we are not to change that
and we have to create the function getword() which should contain a loop that reads a characters,
store it in a array in the address provided one by one and it should stop for whitespace (tab, space, eof)
basically i have this:
int getword(char *w)
{
char str[255];
int i = 0;
int charCount = 0;
printf("enter your sentence:\n");
gets(str);
/*this was provided by the professor, but i'm not sure how to use it
while (( iochar - getchar()) !=EOF);
*/
for(i = 0; str[i] != '\0'; i++) //loop that checks tab and spaces
{
if(str[i] != ' ' && str[i] != '\t')
{
charCount++;
}
}
printf("your string: '%s' contains %d of letters\n", str, charCount);
return 0;}
right now the output of the program is:
enter your sentence:
hey stockoverflow
your string: 'hey stockoverflow' contains 16 of letters
n=0, s=[]
so i'm saving the string and counting it, but i'm not storing it where i should be.
it should display n=16, s=[hey stockoverflow]
actually it should display n=3, s=[hey]
any help would be appreciated
This may be what you are looking for:
for(i = 0; str[i] != '\0'; i++) //loop that checks tab and spaces
{
if(str[i] != ' ' && str[i] != '\t')
{
charCount++;
}
else
{
str[i] = '\0'; // Terminate str
break; // Break out of the for-loop
}
}
printf("your string: '%s' contains %d of letters\n", str, charCount);
strcpy(w, str); // Add this line to copy str into w (which is s from main)
return strlen(w); // Return the length of the result string
This is my code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char chaCap(char n);
int main()
{
char ch,a;
printf("Enter Sentence\n");
while((ch=getchar()) != '\n')
{
chaCap(ch);
printf("%c",a);
}
printf(" \n");
return 0;
}
void chaCap(char n)
{
if(n >= 'a' && n <='z')
n-=32;
}
I need
Ask user to enter sentence
Convert the sentence to all capital letters and print
Convert the sentence to all lower case letters and print
Convert the each character if it is upper to lower and vice versa.
I made for changing capital letters, but i couldn't make lower and vice versa.
when I made code for 2. and 3, getchar() became nothing...
Use the tolower() function :
while((ch=getchar()) != '\n')
{
printf("%c", tolower(ch));
}
you need to return value in function chaCap(char n),
char chaCap(char n)//need to return char instead of void
{
if(n >= 'a' && n <='z')
n-=32;
return n; // return capital letter here
}
and then get return value when call it in main(),
int main()
{
//...
while((ch=getchar()) != '\n')
{
a = chaCap(ch); // get return value
printf("%c",a);
}
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char *strlwr(char *str){
char *s;
for(s = str; *s; ++s){
if(isupper(*s))
*s = tolower(*s);
}
return str;
}
char *strupr(char *str){
char *s;
for(s = str; *s; ++s){
if(islower(*s))
*s = toupper(*s);
}
return str;
}
char *strvrs(char *str){
char *s;
for(s = str; *s; ++s){
if(islower(*s))
*s = toupper(*s);
else if(isupper(*s))
*s = tolower(*s);
}
return str;
}
int main(void){
int ch;
size_t i = 0, size = 32;
char *str = malloc(size);
printf("Enter Sentence\n");
while((ch=getchar()) != '\n' && ch != EOF){
str[i++] = ch;
str[i++] = '\0';
if(i-- == size){
char *temp = realloc(str, (size += 32));
if(!temp){
fprintf(stderr, "realloc error\n");
free(str);
exit(EXIT_FAILURE);
}
str = temp;
}
}
printf("original : %s\n", str);
printf("vice versa : %s\n", strvrs(str));
printf("upper : %s\n", strupr(str));
printf("lower : %s\n", strlwr(str));
free(str);
return 0;
}
Declare ch as a global variable.
The following program will change all uppercase letters to lower case and all lower case letters to upper case.
Your program needed few modifications to get the desired result.I have made the required changes.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void chaCap(char ch);
char ch;
int main()
{
printf("Enter Sentence\n");
while((ch=getchar()) != '\n')
{
chaCap(ch);
printf("%c",ch);
}
printf(" \n");
return 0;
}
void chaCap(char ch)
{
if(ch >= 'a' && ch <='z')
::ch=ch-32;
else if(ch>='A'&&ch<='Z')
::ch=ch+32;
}