I am new to C programming, and I am trying to figure out how to put a period in between letters. This is my code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void CreateAcronym(char userPhrase[], char userAcronym[]) {
int i;
int count = 0;
for (i = 0; i < strlen(userPhrase); ++i) {
if (i == 0 || userPhrase[i - 1] == ' ') {
if (isupper(userPhrase[i])) {
userAcronym[count++] = userPhrase[i];
}
}
}
userAcronym[count] = '\0';
}
int main() {
char userPhrase[1000];
char userAcronym[20];
fgets(userPhrase, 1000, stdin);
CreateAcronym(userPhrase, userAcronym);
printf("%s\n", userAcronym);
return 0;
}
Example Input: Institute of Electrical and Electronics Engineers
My current Output: IEEE
What the output should be: I.E.E.E.
How do I insert the periods in the printf function so it will output to I.E.E.E.?
Instead of writing userAcronym[count++] = userPhrase[i];, you'd like to add both the character and the period:
userAcronym[count++] = userPhrase[i];
userAcronym[count++] = '.';
Related
So I have written this code and I get the result of letters,words,sentences of a text and I use a formula that calculates the grade of this text
index = 0.0588 * L - 0.296 * S - 15.8
but for some reason when I try a text for example :
There are more things in Heaven and Earth, Horatio, than are dreamt of in your philosophy.
(The grade should be 9 based on the result in CS50 paper)
But i get wrong results like 10 for this example.
I did the calculations in google to see if I do something wrong but it shows me 10 there too.
The code is here:
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main(void)
{
int sentences = 0;
int letters =0;
int words = 0;
string text = get_string("Text: ");
int n = strlen(text);
for(int i = 0; i<n ; i++){
if((text[i]== '.' || text[i]== '?' || text[i]== '!') ){
sentences += 1;
} else if (text[i]== 32){
words += 1;
}else if (isalpha(text[i]))
{
letters += 1;
}
}
float L = ((float)letters / (float)words) *100.00;
float S = ((float)sentences / (float)words) *100.00;
int index = round(0.0588 * L - 0.296 * S - 15.8);
printf("%i\n",letters);
printf("%i\n",words);
printf("%i\n",sentences);
if(index<1)
{
printf("Before Grade 1");
}
else if (index>16)
{
printf("Grade 16+");
}
else
{
printf("(Grade %i)\n",index);
}
}
So the answer is to add a starting point of 1 to words because i doesnt calculates the end word!
int words = 1;
thanks #Gerhardh and #Zakk
Starting C on my own following Kerningan & Ritchie 2nd edition ANSCI book.
Here, I am supposed to return the longest lign of a text.
very early in the book, so few functions are cited.
My program returns the ligns AFTER the longest lign
A lign being only defined by ending with '.'
I'm probably overstepping on this forum with my beginner level, but i'd appreciate the insight :) thanks a lot.
#include <stdio.h>
#include <stdlib.h>
#define MAXLEN 1000
void copcol(char cop[], char retour[]);
int main(void)
{
int nbmax, nbnew, c, i;
nbmax = nbnew = i = 0 ;
char lignmax[MAXLEN] ;
char lignnew[MAXLEN] ;
while((c=getchar()) != EOF)
{
lignnew[i] = c ;
i++;
nbnew++ ;
if( (c == '.') && (nbmax < nbnew) && ( c != '\0'))
{
nbmax = nbnew ;
copcol(lignmax, lignnew);
nbnew = 0 ;
i=0 ;
}
}
if ( nbmax > 0 )
printf("%s",lignmax);
return 0;
}
void copcol(char new[], char max[])
{
int i ;
i=0;
while((new[i] = max[i]) != '\0')
++i;
}
also if a forum is more apropriate for this kind of questions, feel free to let me know.
You need to skip shorter lines when you encounter period (.)
#include <stdio.h>
#include <stdlib.h>
#define MAXLEN 1000
void copcol (char new[], char max[]) {
int i = 0;
while ((new[i] = max[i]) != '\0')
++i;
}
int main (void) {
char lignmax[MAXLEN] ;
char lignnew[MAXLEN] ;
int nbmax, nbnew, c;
nbmax = nbnew = 0 ;
while ( (c = getchar()) != EOF) {
// nbnew also tracks the new-line-len
lignnew[nbnew++] = c ;
if ('.' == c) { // line ended, skip shorter, copy longer line
if (nbnew > nbmax) { // copying only when longer than current max-line
nbmax = nbnew ;
lignnew[nbnew] = '\0'; // terminate string
copcol (lignmax, lignnew);
}
//also ignores shorter-lines
nbnew = 0 ; // new line begins after the period (.)
}
}
if (nbmax > 0)
printf ("MaxLength: %d\n MaxLine: [%s]", nbmax, lignmax);
return 0;
}
Note: Input period(.) terminated lines cannot be longer than (MAXLEN -1), it'll cause buffer-overflow otherwise.
I am participating in a coding challenge that consistes in the following:
We start with a number(size of the string) followed by a string (ex: 5WWFAE) by stdin.
The letters given represent elements, F-fire, W-water, E-earth, A-air. Fire and Water cancel each other and Earth and Air cancel as well.
The goal is to simplify the string by canceling the elements and print it, but you can only cancel adjacent members, for example: "7AFEAWWE" would result in "AWE".
In this challenge the points are given depending on how fast the program runs and how little memory you spend, somehow some people still have more points than me. Could you help-me optimising it?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define stack_is_empty() stack_index == -1
#define stack_push() stack[++stack_index] = c
#define stack_pop() --stack_index
#define can_join() (stack[stack_index] + c == 'F' + 'W') || (stack[stack_index] + c == 'A' + 'E')
void process_string(int len)
{
char c;
char *stack = malloc(len * sizeof(char));
register int stack_index = -1;
register int i = 0;
for (i = 0; i < len; ++i)
{
scanf("%c", &c);
if (stack_is_empty())
{
stack_push();
}
else if (can_join())
{
stack_pop();
}
else
{
stack_push();
}
}
printf("%*.*s\n", stack_index + 1, stack_index + 1, stack);
}
int main()
{
int len;
//* Input:
scanf("%d", &len);
if (len == 0)
{
printf("\n");
return 0;
}
process_string(len);
return 0;
}
how can i make this bruteforce algorithm multithreaded? If i launch this it uses only one cpu. How can i parallelize this? It seems impossible. It's ok with fork or with pthread for me. This code makes a bruteforce to invert an hash, it generates all possibile strings, makes the hash and compares with the digest.
#include <stdio.h>
#include <string.h>
#include "attacchi.h"
#include "hash.h"
void iterazione(char *stringa, int index, int lunghezza);
char *checksum,*hashType;
void bruteforce(char digest[],char tipohash[]) {
checksum = malloc(sizeof(char)*1024);
hashType = malloc(sizeof(char)*1024);
strcpy(checksum,digest);
strcpy(hashType,tipohash);
int lunghezza, i;
printf("Inserire la lunghezza massima da testare: ");
scanf("%d", &lunghezza);
char stringa[lunghezza + 1];
memset(stringa, 0, lunghezza + 1);
for (i = 1; i <= lunghezza; i++) {
iterazione(stringa, 0, lunghezza);
}
}
void iterazione(char *stringa, int index, int lunghezza) {
char c;
if (index < (lunghezza - 1)) {
for (c = ' '; c <= '~'; ++c) {
stringa[index] = c;
iterazione(stringa, index + 1, lunghezza);
}
} else {
for (c = ' '; c <= '~'; ++c) {
stringa[index] = c;
stringa[index+1] = '\n';
if(strcmp(hash(stringa,hashType),checksum)==0) {
printf("Trovato!\nhash %s %s -> %s\n", checksum, hashType, stringa);
exit(0);
}
}
}
}
The simplest way to modify your algorithm to take advantage of multiple threads would be instead of starting the recursion with a call to iterazione(stringa, 0, lunghezza), start it with a call that selects the first character from a range then calls iterazione(stringa, 1, lunghezza):
void avvia_iterazione(char iniziale, char finale, int lunghezza)
{
char c;
char stringa[lunghezza + 1] = "";
for (c = iniziale; c <= finale; ++c) {
stringa[0] = c;
iterazione(stringa, 1, lunghezza);
}
}
You then call avvia_iterazione() with different, non-overlapping ranges of characters in each thread (eg. if you have two threads, you might call avvia_iterazione(' ', 'O'); in one and avvia_iterazione('P', '~'); in the other.
(You'll need to make sure that lunghezza >= 2 as well).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
please, how I can convert this:
char infix[] = "123+354*87/156=" (can be variable)
How to separate number's from this string (to integers, like 123 354 87 156, no to 1 2 3 3 5 4...) and char (chars + * / ).
I suppose you need to build a simple calculator... If you're planning to do this from scratch, you gonna need some background from Compiling Theory, and use concepts like Finite State Machine, Parsing, etc.
But there are a lot of tools that can make this task easier: lex/yacc (C), flex/bison (C++) or COCO/R (many languages).
This is a SIMPLE example in C that splits the string in numbers (state=NUM) and symbols (state=SYM):
#include <string.h>
#include <ctype.h>
#define NONE 0
#define NUM 1
#define SYM 2
int _tmain(int argc, char* argv[])
{
char infix[] = "123+354*87/156=";
char buffer[10];
int i, j;
int state = NONE;
char c;
i = 0;
j = 0;
while(i < strlen(infix)) {
c = infix[i];
switch(state) {
case NUM:
if ( isdigit(c) ) {
buffer[j++] = c;
buffer[j] = 0;
i++;
}
else {
printf("%s\n", buffer);
j = 0;
state = NONE;
}
break;
case SYM:
i++;
printf("%c\n", c);
state = NONE;
break;
case NONE:
if ( isdigit(c) ) state = NUM;
else state = SYM;
break;
}
}
getchar();
return 0;
}
When you use C++ and each operator has a length of one char, and string has the form "number operator number operator ... number operator" (that means begins with number, ends with operator and switches always between both) then use istringstream:
#include <sstream>
using namespace std;
void main()
{
char infix[] = "123+345*87/156=";
istringstream is(infix);
double nums[999]; // maybe you need more than 999
char chars[999];
int nums_pos = 0;
int chars_pos = 0;
bool number = true; // begin with number
while (!is.eof())
{
if (number)
{
is >> nums[nums_pos];
nums_pos++;
number = false;
}
else
{
is >> chars[chars_pos];
chars_pos++;
number = true;
}
}
// you got nums_pos-1 numbers and chars_pos chars
}
Here's yet another possible way but in C as you asked...
#include <stdio.h>
main()
{
char infix[] = "123+354*87/156=";
int curVal = 0;
// For each character in infix
for(char *p = infix; *p != '\0'; ++p)
{
// If it is not a ascii numeral
if(*p > '9' || *p < '0')
{
// Output value
printf("%d\n", curVal/10);
// Output char
printf("%c\n", *p);
curVal = 0;
}
else
{
// Accumulate the individual digits
curVal += (*p) - '0';
curVal *= 10;
}
}
}
It will output:
123
+
354
*
87
/
156
=
You can do this as
#include<stdio.h>
int main()
{
char infix[] = "123+354*87/156=";
char *p = infix;
while(1)
{
if(*p == '\0')
break;
if(*p == '+' || *p == '*' ||*p == '/' ||*p == '=' || *p == '-')
{
printf(" ");
}
else
printf("%c", *p);
p++;
}
printf("\n");
return 0;
}
Output:
123 354 87 156