请帮我解决部分功能。在函数中我创建了一个for循环,它应该计算字符串中每个单词开头的合适字母。例如,如果我将字符串“Beautiful skies”传递给函数和char的'',则在for循环之后变量“count”必须等于1,但它不会发生。有什么问题?我在编码方面相当新手,但是这个对我来说是正确的,但由于某种原因,这个for循环不能像我预期的那样工作(这只是整个函数的一部分,但问题在于for循环,因为它总是返回NULL,即使有一些合适的字母):
char** rearrange_string(char *str, char letter, int *size)
{
char *search, **array, upper_letter = toupper(letter), *shift;
int count = 0, i=0, j=0, counter;
for (search = str; *search != '\0'; search++) {
if (*search == ' ') {
continue;
}
if (*search != letter || *search != upper_letter) {
while (*search != ' ') {
search++;
if (*search == '\0') {
break;
}
}
continue;
}
if (*search == letter || *search == upper_letter) {
count++;
while (*search != ' ') {
search++;
if (*search == '\0') {
break;
}
}
}
}
if (count == 0) {
printf("There is no suitable data. Please reinput the string.");
return NULL;
}
答案 0 :(得分:2)
*search != letter || *search != upper_letter
如果true
,总是letter != upper_letter
将其更改为:
*search != letter && *search != upper_letter
答案 1 :(得分:0)
使用字符串 Dim data As String = """grant_type"": ""client_credentials"""
Dim postdata As Byte() = Encoding.UTF8.GetBytes(data)
Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create(uploadURL), HttpWebRequest)
req.Method = "POST"
req.ContentType = "application/json;encoding=utf-8"
req.ContentLength = postdata.Length
req.Accept = "application/json"
req.Credentials = New NetworkCredential(userID, userPW)
req.Timeout = 600000
req.Headers.Add("Authorization", userID & ":" & userPW)
运行时,程序缓冲区溢出,因为"Beautiful skies"
超出了原始字符串。在search
的情况下,您将*search == letter
递增到search
,然后*search==0
在循环结束时再次递增,因此它超出了分配的缓冲区。
以下分析显示:
答案 2 :(得分:0)
谢谢你们!问题出在for循环的第一个if语句中。我应该把&&而不是||。谢谢!如果有人想看整个程序,这里是一个完整的代码:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
char** rearrange_string(char *str, char letter, int *size);
void main()
{
int size=0, i=0;
char *str, letter, **array;
printf("Enter the string and press ENTER to check: ");
gets(str);
printf("Enter the character for check (only lower chars!): ");
scanf("%c", &letter);
array = rearrange_string(str, letter, &size);
if (array == NULL)
{
printf("\nNo data found, please relaunch the program.");
return;
}
printf("The output string(s) is(are):\n");
for (i=0; i<size; i++)
{
printf("%s", array[i]);
printf("\n");
}
printf("size: %d", size);
for (i=0; i<size; i++)
{
free(array[i]);
}
free(array);
}
char** rearrange_string(char *str, char letter, int *size)
{
char *search, **array, upper_letter = toupper(letter), *shift;
int count = 0, i=0, j=0, counter;
for (search = str; *search != '\0'; search++)
{
if (*search == ' ')
{
continue;
}
if (*search != letter && *search != upper_letter)
{
while (*search != ' ')
{
search++;
if (*search == '\0')
{
break;
}
}
continue;
}
if (*search == letter || *search == upper_letter)
{
count++;
while (*search != ' ')
{
search++;
if (*search == '\0')
{
break;
}
}
}
}
if (count == 0)
{
printf("There is no suitable data. Please reinput the string.");
return NULL;
}
*size = count;
array = (char**)malloc(count*sizeof(char*));
for (search = str; *search != '\0'; search++)
{
if (*search == ' ')
{
continue;
}
if (*search != letter && *search != upper_letter)
{
while (*search != ' ')
{
search++;
if (*search == '\0')
{
break;
}
}
continue;
}
if (*search == letter || *search == upper_letter)
{
counter = 1;
j=0;
shift = search;
while (*search != ' ')
{
counter++;
search++;
if (*search == '\0')
{
break;
}
}
array[i] = (char*)malloc(counter*sizeof(char));
while (*shift != ' ')
{
*(*(array+i)+j) = *shift;
j++;
shift++;
if (*shift == '\0')
{
break;
}
}
*(*(array+i)+j) = '\0';
i++;
}
}
return array;
}
答案 3 :(得分:-1)
这样的功能怎么样?:
Installing C# dependencies...
Platform: darwin, x86_64
Downloading package 'OmniSharp for OSX' (24026 KB) .................... Done!
Downloading package '.NET Core Debugger (macOS / x64)' (44057 KB) .................... Done!
Installing package 'OmniSharp for OSX'
Installing package '.NET Core Debugger (macOS / x64)'
Finished
此代码将返回字符串中找到的字符数,例如:
printf(“%li \ n”,count_char(“美丽的天空”,“s”));
将打印:
1