Experiment 8 character data and string

1121 Define an array for storing character strings

Fill in the statement defining the character array in the following program to make the program complete.
#include "stdio.h"
#include "string.h"
int main()
{   char s[80]; /*define a array named s to store string*/
    strcpy(s, "abcdefghijklmn");
    printf("%s", s);
    return 0;
}

1122 Combination of character strings

Input 3 strings from the keyboard (each string ends with a carriage return), merge the 3 strings into the string s in the order of input, please fill in the blanks and use the program to complete.
#include "stdio.h"
#include "string.h"
main()
{
    char s[100]="";
    char a[30];
    gets(a);strcat(s,a);​//strcat concatenates two char types.​
    gets(a);strcat(s,a);//
    gets(a);strcat(s,a);//
    printf("%s", s);
}

1123 String input and output

The program below realizes reading in strings from the keyboard, and then outputting them to the screen, please fill in the necessary sentences.
input sample
Wang
 output sample
What's your name?
Your name is Wang
#include "stdio.h"
main()
{   char  s[50];
    printf("What's your name?\n");
    scanf("%s",s);/*iput your name from the keyboard*/
    //gets(s); input is also available. Note that the output is %s, not %c
    printf("Your name is ");
    printf("%s", s);  /*output your name*/
}

1145 palindrome

Read in a line of strings (no more than 80 characters, terminated by a carriage return), and determine whether the string is a palindrome (that is, spelling from left to right and from
 The spelling is the same from right to left), if yes, output Y, if not, output N.
input format
 a line of string
 output format
 If yes, output Y, if not, output N
 input sample
abba
 output sample
Y
 hint
 or
input:abcba
output:Y
 or
input:abc
output:N
#include "stdio.h"
#include "string.h"
main()//use strlen
{
	int i,j,flag=1;
    char s[100];
    scanf("%s",s);
    for(i=0, j=strlen(s)-1;i<j; i++, j--)
        if(s[i]!=s[j])
        {
            flag=0;
            break;
        }
   if(flag==1)
        printf("Y");
    else
        printf("N");
}

1050 Find string

Input two strings from the keyboard (assuming that the first string must contain the second string, such as the first string is ABCDEF, the second is CDE,
Then CDE is included in ABCDEF), and now the programming is required to output the position where the second string appears in the first line of string.
(If the second string appears more than once in the first string, the first occurrence will prevail)
input sample
ABCDEFG
DE
 output sample
4
 Hint: because DE appears at the 4th character in ABCDEFG
//Standard schedule
#include  "stdio.h"
main()
{   int i,j;
    char a[80], b[80];
    gets(a); gets(b);
    for(i=0;a[i]!='\0';i++)
    {    for(j=0;b[j]!='\0';j++)
             if(a[i+j]!=b[j]) 
                break;//When the corresponding values ​​of a and b are not equal, jump out of the inner loop and start the comparison of the next number in a
         if(b[j]=='\0') break;//When the values ​​in b are all equal, jump out of the outer loop
    }
    if (a[i]!='\0')
     printf("%d",i+1);//As long as a is not i, the number of comparisons is over, and the value of i is about to be empty, there is an equality
    else printf("Not Found");
}
#include <stdio.h>
#include <string.h>
int main()
{
   char a[100];char b[100];
int i=0,r=0,n1=0,n2=0,x=0,judge=0;
gets(a);
gets(b);
n1=strlen(a);n2=strlen(b);
for(i=0;i<n1;i++){
    if(b[0]==a[i]){
    r=i+1;
        while(b[x]==a[i+x]){
        x++;
        judge++;
        if(judge==n2){
        printf("%d",r);
        }
}
}
}
}

1 Judgment of palindrome

A palindrome string means that a character string read from left to right is the same as read from right to left. Now enter a line of characters from the keyboard (up to 80 characters, and end with carriage return), and judge whether it can pass 0 Or 1 character, making the string a palindrome, if it can output Y, otherwise output N
input format
line of characters
output format
Y or N
input sample
abca

output sample
Y
Prompt: Prompt, remove the c character to become a palindrome string

//should be right
/*0 1 2 . . . i i+1 ...... j-1 j .....n In the case of satisfying i+1<j-1, sweep both ends to the middle
a[i]==a[j],Then continue; if a[i]==a[j-1] or a[i+1]=a[j], you need to insert,
Make a mark that needs to be inserted (such as counting), and move the corresponding pointer to skip the unequal characters at the same time, and continue to compare,
Until the leading edge of the two pointers meet and pass through (the leading edge can meet and pass through to indicate that it is less than 4 characters).*/
#include "stdio.h"
#include "string.h"
int main()
{
int n;
char a[100];
 scanf("%s",a);
int i,j,flag;
n=strlen(a);
i=0;
j=n-1;
flag=0; //Make a marker that needs to be inserted
while ( (flag<2) // only need 0 or 1 character
&& ((i+1)<=(j-1)) // have character to compare
)
{
if (a[i]==a[j]) {i++; j--;}
else if (a[i]==a[j-1]) {flag++; i++; j-=2;}
else if (a[i+1]==a[j]) {flag++; i+=2; j--;}
else flag=2;
}
if (flag<2) printf("Y");
else printf("N");
return 0;
}
//Incorrect
#include "stdio.h"
#include "string.h"
main()//use strlen
{
	int i,j,flag=1,k;
    char s[100];
    scanf("%s",s);
    for(i=0, j=strlen(s)-1;i<j; i++, j--)
     {

        if(s[i]!=s[j])
        {
            flag=0;
            break;
        }
     }
   if(flag==1)
       {
        printf("Y");
        return 0;
       }
    else
        for(k=0,flag=1;k<strlen(s)-1;k++)
    {
         for(i=0, j=strlen(s)-1;i<j; i++, j--)
     {
         if(i==k)
            continue;
         else if(s[i]!=s[j])
        {
            flag=0;
            break;
        }
     }
    }
        if(flag==1)
        printf("Y");
        else
        printf("N");
}

Tags: C C++ programming language

Posted by tijger on Sun, 11 Dec 2022 11:01:30 +0300