0

The below code tries to manipulate several lines of text one at the time.

1.My first issue is to write a loop to read several lines of text(using scanf()) and quit when the first character typed is a newline. These lines of text have some conditions: The first character must be a number between 2 and 6 followed by a space and a line of text(<80).This number will make "dance" the text.

2.My second issue is to figure out how to convert the letters from small to capital and viceversa according to the first number typed. I have to function to make these conversions but I don't know how to call them to change the text.For example: if I typed "3 apples and bananas" the correct output should be "AppLes And BanNas".As you see, the white spaces are ignored and the text always start with a capital letter.

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <ctype.h>

using namespace std;
void print_upper(string s1);
void print_lower(string s2);
void main(void)
{
    char text[80];
    text[0]='A';//Initialization
    int count_rhythm;

    while (text[0] != '\n'){//To make the loop run until a newline is typed
        scanf(" %79[^\n]",text);
        if(isdigit(text[0])) //To verify that the first character is a number
        {   
            printf("\nGood");//Only to test 
        }
        else
        {
            printf("\nWrong text\n");//Only to test
        }
    }
}

void print_upper(string s1)//Print capital letters
{
    int k1;
    for(k1=0; s1[k1]!='\0'; ++k1)
        putchar(toupper(s1[k1]));
}

void print_lower(string s2)//Print small letters
{
    int k2;
    for(k2=0; s2[k2]='\0'; ++k2)
        putchar(tolower(s2[k2])); 
}
4

2 回答 2

0

您还可以定义一个函数,该函数printNthUpper()将采用字符串和整数 n 来指定要以大写形式打印的字符。该函数将是一个类似于您已经拥有的函数的循环,但具有比较提供的整数值和给定字母的索引以决定是否调用 toupper() 的条件(例如printf("%c", i%n == 0 ? toupper(s[i]) : s[i]);)。

于 2013-08-31T14:49:47.443 回答
0

要编写一个循环来读取多行文本,您可以将基于条件的无限循环fgets结合使用,而不是使用 scanf。

char line[80];
char result[80] 

while(1)
{
   fgets(line,sizeof(line),stdin); //read line with fgets
   puts(line);

   if(line[0]=='\n')
      break;

   if((strlen(line)>=4)  &&'2'< =line[0] && line[0] <= '6' && line[1]==' ')
   {
      strcpy(result,change_case_of_nth_char(line));// call change case of nth letter 
   }
   else
   {
      //prompt user to enter input again
   }

}

char *change_case_of_nth_char(char *str)  

{ 

}
于 2013-08-31T14:58:33.223 回答